我正在尝试使用聚合但它无法找到索引变量。
df1 <- iris
agg1 <- aggregate(df1, by = list(Species), FUN = sum, na.rm = T, data = df1)
Error in aggregate.data.frame(df1, by = list(Species), FUN = sum, na.rm = T, :
object 'Species' not found
有什么建议吗?
答案 0 :(得分:1)
agg1 <- aggregate(df1[-5], by=list(df1$Species), FUN=sum, na.rm=T)
agg1
# Group.1 Sepal.Length Sepal.Width Petal.Length Petal.Width
#1 setosa 250.3 171.4 73.1 12.3
#2 versicolor 296.8 138.5 213.0 66.3
#3 virginica 329.4 148.7 277.6 101.3
不要忘记从聚合中取出非数字列。并使用df1$Species
或with(df1, aggregate(df1[-5], by=list(Species)..)
指定列。如果您没有像评论中那样使用公式方法,则data=df1
也是不必要的。
答案 1 :(得分:0)
你需要
detach(df)
发布数据列。