How to add tapply results to an existing data frame [duplicate]
问题 This question already has answers here : Calculating statistics on subsets of data [duplicate] (3 answers) Closed 3 years ago . 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