tapply

How to add tapply results to an existing data frame [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-11-28 05:17:42
问题 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

Multiple functions in a single tapply or aggregate statement

扶醉桌前 提交于 2019-11-27 01:53:28
问题 Is it possible to include two functions within a single tapply or aggregate statement? Below I use two tapply statements and two aggregate statements: one for mean and one for SD. I would prefer to combine the statements. my.Data = read.table(text = " animal age sex weight 1 adult female 100 2 young male 75 3 adult male 90 4 adult female 95 5 young female 80 ", sep = "", header = TRUE) with(my.Data, tapply(weight, list(age, sex), function(x) {mean(x)})) with(my.Data, tapply(weight, list(age,

Grouping functions (tapply, by, aggregate) and the *apply family

女生的网名这么多〃 提交于 2019-11-25 22:12:20
问题 Whenever I want to do something \"map\"py in R, I usually try to use a function in the apply family. However, I\'ve never quite understood the differences between them -- how { sapply , lapply , etc.} apply the function to the input/grouped input, what the output will look like, or even what the input can be -- so I often just go through them all until I get what I want. Can someone explain how to use which one when? My current (probably incorrect/incomplete) understanding is... sapply(vec, f