r - Count observations of distinct values per group and add a new column of counts for each value -
this question has answer here:
- create columns factors , count 2 answers
similar question count number of observations/rows per group , add result data frame not quite.
i'd transform this
group id_in_group letter 1: a1 alef 2: a2 bet 3: a3 bet 4: b b1 alef 5: b b2 alef 6: b b3 gimel
into this
group aleph bet gimel 1: 1 2 0 2: b 2 0 1
or without additional library, can use table:
table(df$group,df$letter)
as seem work data.table, can use dcast()
dcast(df, group~letter,length)
Comments
Post a Comment