r - 按列求和并使用 R 计算百分比
问题描述
我有一个如下所示的数据表。我想获得如下输出。(创建一个名为“百分比”的行并简单地计算每年总和的“S”百分比)。请参阅下面的输出表。
我怎样才能实现这是R数据表方法?
任何帮助表示赞赏。
Table:
Category 1998 1999 2000 2001 2002 ..... 2018
No_History 10 15 2 22 15 ..... 16
NS 17 20 15 23 10 ..... 21
S 15 14 85 25 47 ...... 15
Output:
Category 1998 1999 2000 2001 2002 ..... 2018
No_History 10 15 2 22 15 ..... 16
NS 17 20 15 23 10 ..... 21
S 15 14 85 25 47 ..... 15
Percentage 35.7 28.5 83.3 35.7 65.2 ..... 28.8
Simply calculate percentage = S/(No_History+NS+S)*100
解决方案
也许是这样的。首先,我创建一个数据框。
# Create data frame
df <- read.table(text ="Category 1998 1999 2000 2001 2002 2018
No_History 10 15 2 22 15 16
NS 17 20 15 23 10 21
S 15 14 85 25 47 15", header = FALSE)
然后,我必须将其重组为有用的格式。使用整洁的格式让生活更轻松。
# Restructure data:
# Transpose
# Use first row as column names
# Remove first row
# Convert to data table
# Convert columns to numerics
df <- t(df)
colnames(df) <- df[1, ]
df <- df[-1,]
dt <- data.table(df)
dt[, names(dt) := lapply(.SD, as.numeric)]
最后,我进行计算:
# Do calculation
dt[, Percentage := 100 * S/(No_History + NS + S)]
给予,
# Category No_History NS S Percentage
# 1: 1998 10 17 15 35.71429
# 2: 1999 15 20 14 28.57143
# 3: 2000 2 15 85 83.33333
# 4: 2001 22 23 25 35.71429
# 5: 2002 15 10 47 65.27778
# 6: 2018 16 21 15 28.84615
要将数据恢复为您指定的格式,我必须转置数据表。
# Transpose back to desired format
t(dt)
# [,1] [,2] [,3] [,4] [,5] [,6]
# Category 1998.00000 1999.00000 2000.00000 2001.00000 2002.00000 2018.00000
# No_History 10.00000 15.00000 2.00000 22.00000 15.00000 16.00000
# NS 17.00000 20.00000 15.00000 23.00000 10.00000 21.00000
# S 15.00000 14.00000 85.00000 25.00000 47.00000 15.00000
# Percentage 35.71429 28.57143 83.33333 35.71429 65.27778 28.84615
如果data.table
不是必须的,您也可以使用dplyr
, 。
# Create data frame
df <- read.table(text ="Category 1998 1999 2000 2001 2002 2018
No_History 10 15 2 22 15 16
NS 17 20 15 23 10 21
S 15 14 85 25 47 15", header = FALSE)
# Restructure data:
# Transpose
# Use first row as column names
# Remove first row
df <- t(df)
colnames(df) <- df[1, ]
df <- df[-1,]
# Convert to data frame
# Convert all to numeric
# Perform calculation
# Transpose result
df %>%
data.frame %>%
mutate_all(function(x)as.numeric(as.character(x))) %>%
mutate(Percentage = 100 * S /(No_History + NS + S)) %>%
t
# [,1] [,2] [,3] [,4] [,5] [,6]
# Category 1998.00000 1999.00000 2000.00000 2001.00000 2002.00000 2018.00000
# No_History 10.00000 15.00000 2.00000 22.00000 15.00000 16.00000
# NS 17.00000 20.00000 15.00000 23.00000 10.00000 21.00000
# S 15.00000 14.00000 85.00000 25.00000 47.00000 15.00000
# Percentage 35.71429 28.57143 83.33333 35.71429 65.27778 28.84615
推荐阅读
- java - 按子搜索用户返回 InvalidParameterException
- android - Android小部件RemoteViewsFactory的getViewAt()方法随机调用
- sql-server - 为员工获取不同的范围
- python - 如何在多线程情况下使用 python 线程锁模块
- arrays - 考虑到VBA矩阵中的所有可能组合,如何将不同的值相乘?
- android - Android 应用程序时向 Native Code 发送信息。开始
- mysql - 将两个 python mysql 查询与条件结合起来
- python-3.x - 我知道这个问题已经被问过了,但我很好奇这与我的问题有什么特别的关系
- java - 在回收站视图的项目视图中更改可绘制渐变的颜色
- javascript - 如何仅使用 Javascript 减慢平滑滚动的默认速度?