如何计算分配给类别中个人的变量的平均值? [英] How to make an average of a variable assigned to individuals within a category?
问题描述
我有一个大数据集,可以这样表示:
I have a big data set which could be represented something like this:
plot 1 2 3 3 3 4 4 5 5 5 5 6 7
fate S M S S M S S S M S S M M
其中地块是一个地点,而命运是生存"或死亡"(植物生存或死亡.)植物的地块编号与其下的命运相对应.因此,在地块5中有4种植物.其中3个幸存,1个死亡.
where plot is a location, and fate is either "survivorship" or "mortality" ( a plant lives or dies.) The plot number of a plant corresponds to the fate under it. Thus in plot 5 there are 4 plants. 3 of them survive, 1 dies.
我想找出一种方法,使R计算所有这些情节在每个地块中幸存的个体的比例.事实证明,这非常具有挑战性.
I want to figure out a way to make R calculate the fraction of individuals that survive in each plot for all of these. It is proving very challenging.
示例:情节5将返回3/4或75%的生存值 情节3将返回2/3或66%的生存值
Example: Plot 5 would return a survivorship value of 3/4 or 75% Plot 3 would return a survivorship value of 2/3 or 66%
任何帮助将不胜感激. 谢谢
Any help would be much appreciated. Thank you
structure(list(plot = c(1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5, 6, 7
), fate = structure(c(2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L,
2L, 1L, 1L), .Label = c("M", "S"), class = "factor")), .Names = c("plot",
"fate"), row.names = c(NA, -13L), class = "data.frame")
推荐答案
这里是dplyr
的一种解决方案;我创建了valu
列,如果幸存则为1,否则为0.之后,仅需对1进行求和,然后将其除以图的元素总数即可.
Here is one solution with dplyr
; I've created valu
column with 1 if survived and 0 if not. After that it is only a matter of sum the 1's and divide them by the total number of elements of plot.
library(dplyr)
df %>% group_by(plot) %>%
mutate(valu = ifelse(fate == "S", 1, 0)) %>%
mutate(perce = (sum(valu)/n() )*100 )
Source: local data frame [13 x 4]
Groups: plot
plot fate valu perce
1 1 S 1 100.00000
2 2 M 0 0.00000
3 3 S 1 66.66667
4 3 S 1 66.66667
5 3 M 0 66.66667
6 4 S 1 100.00000
7 4 S 1 100.00000
8 5 S 1 75.00000
9 5 M 0 75.00000
10 5 S 1 75.00000
11 5 S 1 75.00000
12 6 M 0 0.00000
13 7 M 0 0.00000
这篇关于如何计算分配给类别中个人的变量的平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!