简体   繁体   English

如何基于现有数据在R中创建百分比表?

[英]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.

相关问题 如何在R中的现有数据集中添加一个名为“百分比”的新列。百分比是下表中提到的6项考试的平均值? - How do I add a new column named “Percentage” to the existing data set in R.Percentage is the average of the 6 exams as mentioned in the table below? 根据R中的现有列创建多个百分比列 - Create multiple percentage columns based on existing columns in R 如何使用 for 循环在 R 中基于两个现有数据框创建数据框列表? - How do I use a for loop to create a list of data frames, based off two existing data frames, in R? 如何在R中创建百分比变化表? - How to create a table of percentage changes in R? R 如何根据现有数据创建列/特征 - R how to create columns/features based on existing data R - 如何使用聚合数据表创建图表? - R - How can i create graphs using an Aggregate data table? 如何获取 R 中一组数据的频率和百分比? - How I can get frequency and percentage for a set of the data in R? 使用R中的现有数据创建和填充新表 - Create and fill new table with existing data in R 如何根据我由 R 生成的表创建新的数据框? - How do I create a new data frame based on a table I generated by R? 根据现有列的子字符串在 R data.table 中创建列 - Create columns in R data.table based on sub-strings of existing column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM