我有一个有两列的数据帧。第一列包含类别,如“第一”,“第二”,“第三”,第二列有数字,表示我从“类别”中看到特定组的次数。
例如:
Category Frequency
First 10
First 15
First 5
Second 2
Third 14
Third 20
Second 3
我想按类别对数据进行排序,并将所有频率相加:
Category Frequency
First 30
Second 5
Third 34
在R中怎么做呢?
使用cast代替reccast(注意'Frequency'现在是'value')
df <- data.frame(Category = c("First","First","First","Second","Third","Third","Second")
, value = c(10,15,5,2,14,20,3))
install.packages("reshape")
result<-cast(df, Category ~ . ,fun.aggregate=sum)
得到:
Category (all)
First 30
Second 5
Third 34
使用cast代替reccast(注意'Frequency'现在是'value')
df <- data.frame(Category = c("First","First","First","Second","Third","Third","Second")
, value = c(10,15,5,2,14,20,3))
install.packages("reshape")
result<-cast(df, Category ~ . ,fun.aggregate=sum)
得到:
Category (all)
First 30
Second 5
Third 34