beginneR
beginneR

Reputation: 3291

data preparation part II

there's another problem I encountered which is (as I think) quite interesting:

dt <- data.table(K=c("A","A","A","B","B","B"),A=c(2,3,4,1,3,4),B=c(3,3,3,1,1,1))
dt
   K  A  B
1: A  2  3
2: A  3  3
3: A  4  3
4: B  1  1
5: B  3  1
6: B  4  1

Now I want a somewhat "higher" level of the data. For each letter in K, there should only be one line and "A_sum" should include the length of A where B has the same value. So there are three values for B=3 and three values for B=1.

Resulting data.table:

dt_new
   K A_sum B
1: A     3 3
2: B     3 1

Upvotes: 1

Views: 83

Answers (1)

alexwhan
alexwhan

Reputation: 16026

It's not clear how you want to treat K, but here's one option:

dt_new <- dt[, list(A_sum = length(A)), by = list(K, B)]
#    K B A_sum
# 1: A 3     3
# 2: B 1     3

Upvotes: 4

Related Questions