Christopher Flach Christopher Flach - 1 month ago 16
R Question

R Combining Rows with Factors and Numeric Values

I'm trying to combine rows in a data frame with factors and numeric values.
Here's an example similar to what I'm trying to do.

Letter Freq
a .1
b .2
c .3
d .4


I'd like to combine rows 1, 2, and 3 for example and get...

Letter Freq
group .6
d .4


Usually I can find something similar on this site, but I'm not sure if I'm not searching on the right terminology, or what.

Answer

One option would be to change the first three levels in 'Letter' to 'group' and then use aggregate to get the sum of 'Freq' by 'Letter'

levels(df1$Letter)[1:3]  <- "group"
aggregate(Freq~Letter, df1, sum)
#   Letter Freq
#1   group  0.6
#2       d  0.4
Comments