MFR MFR - 27 days ago 7
R Question

Sum per group with a condition in data.table

I wish to sum each group with a condition. I have the following data

a<-data.table (facto=c ("a","b","a","a","b","b","b"), value = c (2,3,2,3,2,2,2))

facto value
1: a 2
2: b 3
3: a 2
4: a 3
5: b 2
6: b 2
7: b 2


and I'd like to compute the sum of facto which their value is not equal to 3 using
data.table
.

I used this code

output <-a[, (value=sum(value!=3)), .(facto)]


and it gives me the following data that counts the row which are not equal to zero for each group:

facto V1
1: a 2
2: b 3


However, I wish to have the following output:

facto V1
1: a 4
2: b 6

Answer

We can specify the logical condition in i and get the sum of 'value' by facto

a[value!=3, sum(value), by = facto]
#   facto V1
#1:     a  4
#2:     b  6

Also, the OP's code is getting the sum of logical index and not the value after subsetting

a[, (value=sum(value[value!=3])), .(facto)]