【发布时间】:2016-05-27 17:48:10
【问题描述】:
问题
创建一个包含百分比的新行
数据
df<- data.frame(
species = c ("A","A","A","A","B","B","B","B","A","A","A","A","B","B","B","B"),
number = c(1,1,2,2,1,1,2,2,1,1,2,2,1,1,2,2),
treatment = c(0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1),
variable = c ("x","y","x","y","x","y","x","y","x","y","x","y","x","y","x","y"),
value = sample(1:16)
)
问题
我想计算给定数量和处理的物种的百分比......即变量 x 和 y(前两行)的总和应为 100%。
我用 dplyr 试过了:
result <- df%>%
group_by(variable) %>%
mutate(percent = value*100/sum(value))
test<-subset(result,variable=="x")
sum(test[,6]) # sums to 100%
“测试”是错误的,因为它是两个物种和两个治疗中所有 x 的百分比。
期望的输出
species number treatment variable value percent
A 1 0 x 40 40
A 1 0 y 60 60
A 2 0 x 1 10
A 2 0 y 9 90
【问题讨论】:
-
你需要
df %>% group_by(variable) %>% mutate(percent= value*100/sum(df$value)) -
不,那只是我的尝试。任何解决方案都可以..
-
我的意思是
sum(df$value)而不是sum(value) -
比较@akrun 的方法和你的方法的输出:它们是不同的。按照您描述的方式,akrun 的方法为您提供了正确的解决方案。
-
当您使用
sample时,请使用set.seed以便它可以重现。