5

I have this dataframe :

> head(merged.tables)
  Store DayOfWeek       Date Sales Customers Open Promo StateHoliday SchoolHoliday StoreType
1     1         5 2015-07-31  5263       555    1     1            0             1         c
2     1         6 2013-01-12  4952       646    1     0            0             0         c
3     1         5 2014-01-03  4190       552    1     0            0             1         c
4     1         3 2014-12-03  6454       695    1     1            0             0         c
5     1         3 2013-11-13  3310       464    1     0            0             0         c
6     1         7 2013-10-27     0         0    0     0            0             0         c
  Assortment CompetitionDistance CompetitionOpenSinceMonth CompetitionOpenSinceYear Promo2
1          a                1270                         9                     2008      0
2          a                1270                         9                     2008      0
3          a                1270                         9                     2008      0
4          a                1270                         9                     2008      0
5          a                1270                         9                     2008      0
6          a                1270                         9                     2008      0
  Promo2SinceWeek Promo2SinceYear PromoInterval
1              NA              NA              
2              NA              NA              
3              NA              NA              
4              NA              NA              
5              NA              NA              
6              NA              NA       

Then I want to extract a dataframe showing the average of Sales vector when Open equal to 1 and by StoreType. I used this command because it's the fatest I think:

merged.tables[StateHoliday==1,mean(na.omit(Sales)),by=StoreType]

But I got this error:

Error in [.data.frame(merged.tables, StateHoliday == 0, mean(na.omit(Sales)), : unused argument (by = StoreType)

I search but I didn't get an answer to this error. Thanks for your help!

smci
  • 32,567
  • 20
  • 113
  • 146
A2018
  • 77
  • 1
  • 1
  • 4
  • 7
    You are using the syntax of `[.data.table`. It seems that `merged.tables` is not a data.table-object. Use `setDT()` – jogo Feb 09 '18 at 21:56
  • can you use `dput` to allow us to recreate your data? – Kamil Feb 09 '18 at 22:43

2 Answers2

8

I had such an error.

Problem was solved when I realised: my data was not in data.table format.

example: copy <- data.table(data)

Alexander Moroz
  • 119
  • 1
  • 6
1

Overview

There are lots of ways of applying a function to a group of values in your data frame. I present two:

  1. Using the dplyr package to arrange your data in a way that answers your question.
  2. Using tapply(), which performs a function over a group of values.

Reproducible Example

For each store type, I want the average sales for those stores whose Open value is equal to 1.

I present the method first, followed by .

Note: The following data frame only takes a few columns from those posted in the OP.

# install necessary package
install.packages( pkgs = "dplyr" )

# load necessary package
library( dplyr )

# create data frame
merged.tables <-
  data.frame(
    Store = c( 1, 1, 1, 2, 2, 2 )
    , StoreType = rep( x = c( "s", "m", "l" ) , times = 2)
    , Sales = round( x = runif( n = 6, min = 3000, max = 6000 ) , digits = 0 )
    , Open = c( 1, 1, 0, 0, 1, 1 )
    , stringsAsFactors = FALSE
  )

# view the data
merged.tables
#   Store StoreType Sales Open
# 1     1         s  4608    1
# 2     1         m  4017    1
# 3     1         l  4210    0
# 4     2         s  4833    0
# 5     2         m  3818    1
# 6     2         l  3090    1

# dplyr method
merged.tables %>%
  group_by( StoreType ) %>%
  filter( Open == 1 ) %>%
  summarise( AverageSales = mean( x = Sales , na.rm = TRUE ) )
# A tibble: 3 x 2
#   StoreType AverageSales
#   <chr>            <dbl>
# 1 l                 3090
# 2 m                 3918
# 3 s                 4608


# tapply method

# create the condition
# that 'Open' must be equal to one
Open.equals.one <- which( merged.tables$Open == 1 )

# apply the condition to
# both X and INDEX
tapply( X = merged.tables$Sales[ Open.equals.one ]
        , INDEX = merged.tables$StoreType[ Open.equals.one ]
        , FUN = mean
        , na.rm = TRUE # just in case your data does have NA values in the `Sales` column, this removes them from the calculation
)
# l      m      s 
# 3090.0 3917.5 4608.0 

# end of script #

Resources

Should you need more conditions later on, I encourage you to check out other relevant SO posts, such as How to combine multiple conditions to subset a data-frame using β€œOR”? and Why is [ better than subset?.

Cristian E. Nuno
  • 2,822
  • 2
  • 19
  • 33