I would like to add tapply
results to the original data frame as a new column.
Here is my data frame:
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)
I would like to to add the mean of each category to the data frame as a column.
I used: tapply(dat$birds, dat$category, mean)
to get the mean per category but I didn't find a way to add it to the data set in such away that in a new column I'll have the mean of the relevant category.