0

I have the following formulas in excel, but calculation takes forever, so I would like to find a way to calculate these formulas in excel.

I'm counting the number of times an item shows up in a location (Location 1, Location 2, and External) with these formulas:

=SUMPRODUCT(($N:$N=$A2)*(RIGHT(!$C:$C)="1")
=SUMPRODUCT(($N:$N=$A2)*(RIGHT(!$C:$C)="2")
=SUMPRODUCT(($N:$N=$A2)*(LEFT($C:$C)="E"))

Here is the dataframe in which the columns with these values will be added:

> str(FinalPars)
'data.frame':   10038 obs. of  3 variables:
 $ ID: int  11 13 18 22 39 181 182 183 191 192 ...
 $ Minimum     : num  15 6 1.71 1 1 4.39 2.67 5 5 2 ...
 $ Maximum     : num  15 6 2 1 1 5.48 3.69 6.5 5 2 ...

and here is the dataset to which the ItemID will be matched (This is a master list of all locations each item is stored in):

> str(StorageLocations)    
'data.frame':   14080 obs. of  3 variables:
     $ ID                           : int  1 2 3 4 5 6 7 8 9 10 ...
     $ CLASSIFICATION               : Factor w/ 3 levels "Central 1","Central 2",..: 3 3 3 1 2 3 3 1 2 3 ...
     $ Cart Descr                   : Factor w/ 145 levels "Closet1",..: 36 41 110 1 99 58 60 14 99 60 ...

Sample of Storage Location Data Frame:

ID     Classification     Cart Descr
123    Central 1          Main Store Room
123    Central 2          Secondary Store Room
123    External           Closet 1
123    External           Closet 2
123    External           Closet 3

So the output for the above would be added to the data frame total pars as the new colums Central 1, Central 2, and External and count the number of times the item was IDd as in those locations:

ID    Minimum    Maximum   Central 1   Central 2   External
123     10        15          1            1          3

This was my output in Excel - a Count of the # of times an item was identified as Central 1, Central 2, or External

If anyone knows the comparable formula in R it would be great!

Sutton Murray
  • 35
  • 2
  • 7
  • 3
    Possible duplicate of [how to realize countifs function (excel) in R](https://stackoverflow.com/questions/23000661/how-to-realize-countifs-function-excel-in-r) – BLT Aug 28 '17 at 17:43
  • 3
    It would be easier to help if you provided a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and the desired output. – MrFlick Aug 28 '17 at 17:46
  • @BLT That is helpful, but how do you use multiple criteria with that same function? I want to count those equal to the Item as well as whichever location? – Sutton Murray Aug 28 '17 at 17:47

1 Answers1

1

It's hard to know what you are really asking for without example data. I produced an example below.

Location <- c(rep(1,4), rep(2,4), rep(3,4))
Item_Id <- c(rep(1,2),rep(2,3),rep(1,2),rep(2,2),rep(1,3))
Item_Id_Want_to_Match <- 1

df <- data.frame(Location, Item_Id)

> df
   Location Item_Id
1         1       1
2         1       1
3         1       2
4         1       2
5         2       2
6         2       1
7         2       1
8         2       2
9         3       2
10        3       1
11        3       1
12        3       1

sum(ifelse(df$Location == 1 & df$Item_Id == Item_Id_Want_to_Match, df$Item_Id*df$Location,0))

> sum(ifelse(df$Location == 1 & df$Item_Id == Item_Id_Want_to_Match, df$Item_Id*df$Location,0))
[1] 2

EDIT:

ID <- rep(123,5)
Classification <- c("Central 1", "Central 2", rep("External",3))

df <- data.frame(ID, Classification)
df$count <- 1

ID2 <- 123
Min <- 10
Max <- 15

df2 <- data.frame(ID2, Min, Max)

library(dplyr)
count_df <- df %>% 
  group_by(ID, Classification) %>% 
  summarise(count= sum(count))

> count_df
Source: local data frame [3 x 3]
Groups: ID

   ID Classification count
1 123      Central 1     1
2 123      Central 2     1
3 123       External     3

library(reshape)
new_df <- recast(count_df, ID~Classification, id.var=c("ID", "Classification"))
> new_df
   ID Central 1 Central 2 External
1 123         1         1        3

merge(new_df, df2, by.x="ID", by.y="ID2")

> merge(new_df, df2, by.x="ID", by.y="ID2")
   ID Central 1 Central 2 External Min Max
1 123         1         1        3  10  15
Katie
  • 362
  • 3
  • 14
  • 2
    The `ifelse()` isn't really needed, you can directly subset the vector to sum: `with(df, sum((Item_Id * Location)[Item_id == Item_Id_Want_to_Match & Location == 1])` – Gregor Thomas Aug 28 '17 at 18:05
  • I think you mean an & instead of *: with(df, sum((Item_Id * Location)[Item_Id == Item_Id_Want_to_Match & Location == 1])). Yes, that works too! – Katie Aug 28 '17 at 18:09
  • @user108363 Sorry, I added the two dataframes in R I'm working from above. One is a list of all items and I want to add a "count" column, that counts all those in the item master that match the item ID and location (Central 1, Central 2, etc) – Sutton Murray Aug 29 '17 at 11:28
  • @Gregor I've updated with the R Dataframes I'm using - Sorry I forgot to include them. I'm also trying your suggestion now! – Sutton Murray Aug 29 '17 at 12:30
  • @SuttonMurray Can you provide an example of what your output should look like? Also, it would be helpful to have a sample of the actual dataframe. It's hard to tell what Cart Descr contains. – Katie Aug 29 '17 at 12:40