我有一个大数据集可以表示如下:
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人死亡。
我想找出一种方法让R计算出所有这些中每个图中存活的个体的比例。这证明非常具有挑战性。
示例:图5将返回3/4或75%的生存值 图3将返回2/3或66%
的生存值非常感谢任何帮助。 谢谢
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")
答案 0 :(得分:1)
以下是dplyr
的一个解决方案;我创建了valu
列,如果幸存则为1,否则为0。在那之后,它只是1的总和,并将它们除以图的元素总数。
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
答案 1 :(得分:0)
有很多种可能性。这是一个:
plot <- c(1,2,3,3,3,4,4,5,5,5,5,6,7)
fate <- as.factor(c("S","M","S","S","M","S","S","S","M","S","S","M","M"))
sapply(lapply(split(fate, plot), function(x) round(prop.table(table(x))*100, 2 )), "[", "S")
# 1.S 2.S 3.S 4.S 5.S 6.S 7.S
# 100.00 0.00 66.67 100.00 75.00 0.00 0.00