Reputation: 974
I want to find the mean of non-zero values by group for multiple columns:
mydata<- data[, lapply(.SD, function(x) mean(x, na.rm=TRUE)), by=group,
.SDcols=c("col1", "col2")]
What should I add to the above code?
Upvotes: 1
Views: 1538
Reputation: 887691
We subset the elements that are not 0 (x[x!=0]
) for each column specified in .SDcols
and get the mean
by 'group'
data[, lapply(.SD, function(x) mean(x[x!=0], na.rm=TRUE)), by =group,
.SDcols= c('col1', 'col2')]
Upvotes: 3