我的数据集“ data1”看起来像这样
Price class
243 1
32 2
45 3
245 1
67 2
343 3
567 1
.
.
依此类推,在类1,2,3列中会不断重复自身,直到数据结束(298个观察值)。
我想将其汇总,以使我得到每个班级的平均水平。数据应该看起来像。数据应位于新的数据集“ classdata”上
class column_name
1 mean of all class 1 prices
2 mean of all class 2 prices
3 mean of all class 3 prices
我尝试了这段代码
classdata = aggregate(x=data1$Price, by=list(data1$class), FUN="mean")
但是我没有得到理想的结果。请帮忙。
您可能需要适当的列名。为了使它们也放入x=
列表中,并在两个参数中命名列表。
aggregate(x=list(column_name=data1$Price), by=list(class=data1$class), FUN="mean")
# class column_name
# 1 1 351.6667
# 2 2 49.5000
# 3 3 194.0000
数据:
data1 <- structure(list(Price = c(243L, 32L, 45L, 245L, 67L, 343L, 567L
), class = c(1L, 2L, 3L, 1L, 2L, 3L, 1L)), class = "data.frame", row.names = c(NA,
-7L))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句