我想将tapply
结果添加到原始数据帧中作为新列。如何将tapply结果添加到现有数据帧
这里是我的数据帧:
dat <- read.table(text = " category birds wolfs snakes
yes 3 9 7
no 3 8 4
no 1 2 8
yes 1 2 3
yes 1 8 3
no 6 1 2
yes 6 7 1
no 6 1 5
yes 5 9 7
no 3 8 7
no 4 2 7
notsure 1 2 3
notsure 7 6 3
no 6 1 1
notsure 6 3 9
no 6 1 1 ",header = TRUE)
我想补充每个类别的平均数据帧为一列。 我用:tapply(dat$birds, dat$category, mean)
来获得每个类别的平均值,但是我没有找到一种方法将它添加到数据集中,以至于在新的列中我将具有相关类别的均值。
谢谢@akrun它的工作.. – 2014-09-01 11:48:44
@migdal menora很高兴为你工作 – akrun 2014-09-01 11:49:22