我想將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