[英]How can I create a percentage table in R based on existing data?
I would like to know how i can create a new table of percentages based on data from an existing dataframe. 我想知道如何根据现有数据框中的数据创建新的百分比表。
This is aa sample of the data I have been working with: 这是我一直在处理的数据的一个样本:
Sample All 1 2 3 4
1 E1 250 29 27 11 18
2 E2 210 36 24 28 21
3 E3 130 29 10 12 23
4 E4 140 33 24 22 14
5 E5 220 27 16 19 18
This is the dput of my code sample: 这是我的代码示例的输出:
dd <- data.frame(
Sample = c("E1", "E2", "E3", "E4", "E5"),
`All` = c(250, 210, 130, 140, 220),
`1` = c(29, 36, 29, 33, 27),
`2` = c(27, 24, 10, 24, 16),
`3` = c(11, 28, 12, 22, 19),
`4` = c(18, 21, 27, 14, 18),
check.names=FALSE
)
I am trying to calculate the percentages for all the values from column 3:5 against the value in column 2. 我正在尝试针对列2中的值计算列3:5中所有值的百分比。
This is what my desired output looks like: 这是我想要的输出如下所示:
Sample All 1 2 3 4
1 E1 250 11.6 10.8 4.4 7.2
2 E2 210 17.1 11.4 13.3 10
3 E3 130 22.3 7.6 9.2 17.69
I have tried using prop.table however this doesn't give me my desired output 我尝试使用prop.table,但是这并没有给我我想要的输出
You can build the data frame yourself with just extracting columns and doing the division yourself. 您可以只提取列并自己进行划分即可自己构建数据框架。 For example 例如
data.frame(dd[,1:2], dd[,-(1:2)]/dd[,2] * 100, check.names=FALSE)
# Sample All 1 2 3 4
# 1 E1 250 11.60000 10.800000 4.400000 7.200000
# 2 E2 210 17.14286 11.428571 13.333333 10.000000
# 3 E3 130 22.30769 7.692308 9.230769 20.769231
# 4 E4 140 23.57143 17.142857 15.714286 10.000000
# 5 E5 220 12.27273 7.272727 8.636364 8.181818
Here we take the first two columns, then we take all the other columns divided by the second column times 100. 这里我们取前两列,然后取所有其他列除以第二列乘以100。
You can use mutate_at
in dplyr to select the columns you want to change, and provide an anonymous function (formula to be converted to function) of ~ 100*./All
. 您可以在dplyr中使用mutate_at
来选择要更改的列,并提供~ 100*./All
的匿名函数(要转换为函数的公式)。
library(dplyr)
dd %>%
mutate_at(as.character(1:4), ~ 100*./All)
# Sample All 1 2 3 4
# 1 E1 250 11.60000 10.800000 4.400000 7.200000
# 2 E2 210 17.14286 11.428571 13.333333 10.000000
# 3 E3 130 22.30769 7.692308 9.230769 20.769231
# 4 E4 140 23.57143 17.142857 15.714286 10.000000
# 5 E5 220 12.27273 7.272727 8.636364 8.181818
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.