假设我有一个计数表,例如:
library(tidyverse)
set.seed(123)
df<-data.frame(x=letters[1:3],
y=sample(1:10, 3),
z=sample(3:20, 3))%>%
mutate(tot=rowSums(select(., -1)))
df
x y z tot
1 a 3 20 23
2 b 10 13 23
3 c 2 7 9
如何快速生成比例表
x y z tot
1 a 13.04 86.96 100
2 b 43.48 56.52 100
3 c 22.22 77.78 100
您可以使用:
df %>% mutate_if(is.numeric, ~./tot*100)
# x y z tot
# 1 a 13.04348 86.95652 100
# 2 b 43.47826 56.52174 100
# 3 c 22.22222 77.77778 100
无论它们存在于数据库中的什么位置,它只会更新您的数字变量。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句