MFR
MFR

Reputation: 2077

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 3 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

Upvotes: 2

Views: 2608

Answers (1)

akrun
akrun

Reputation: 886938

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)]

Upvotes: 4

Related Questions