0

I have a data frame which consists of numeric and character vectors for which i am trying to sum all vectors and divide it by 4, my code is as below

df2 =  df1%>%
  group_by(city,name,day,hour, age ,sex) %>%
  summarise_if(is.numeric, function(x) sum(x/4,na.rm =T))

how can we write a same code in data.table package , so that we can save the time. Thanks in advance.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
sasir
  • 167
  • 10
  • Thanks Henrik, how can we add sum/4 in mydt[, lapply(.SD, sum), .SDcols = cols] – sasir Nov 21 '17 at 11:11
  • You can pass an "anonymous function" to `lapply`, as described e.g. here: [data.table in R - apply multiple functions to multiple columns](https://stackoverflow.com/questions/29620783/data-table-in-r-apply-multiple-functions-to-multiple-columns) – Henrik Nov 21 '17 at 11:26

0 Answers0