Skip to main content
 首页 » 编程设计

R语言dplyr把数据集的列转为行

2022年07月19日131zengkefu

前文我们学习了spread函数,用于把某个变量根据其值展开为多列;gather函数行为正好相反,用于聚合多个变量,转为行记录。

语法如下:

gather(data, key value, …)

data: 数据框名称
key: 创建变量的名称
value: 值变量(列)的名称
… : 指定哪些列需要聚合

下面举例说明:

 
library(tidyr) 
 
#create data frame 
df <- data.frame(player=c('A', 'B', 'C', 'D'), 
                 year1=c(12, 15, 19, 19), 
                 year2=c(22, 29, 18, 12)) 
 
#view data frame 
# df 
 
#   player year1 year2 
# 1      A    12    22 
# 2      B    15    29 
# 3      C    19    18 
# 4      D    19    12 
 

使用gather()函数聚集第2、3两列:

 
#gather data from columns 2 and 3 
gather(df, key="year", value="points", 2:3) 
 
#   player  year points 
# 1      A year1     12 
# 2      B year1     15 
# 3      C year1     19 
# 4      D year1     19 
# 5      A year2     22 
# 6      B year2     29 
# 7      C year2     18 
# 8      D year2     12 

下面再看一个示例,聚集多个列:

#create data frame 
df2 <- data.frame(player=c('A', 'B', 'C', 'D'), 
                  year1=c(12, 15, 19, 19), 
                  year2=c(22, 29, 18, 12), 
                  year3=c(17, 17, 22, 25)) 
 
#view data frame 
df2 
#  
#   player year1 year2 year3 
# 1      A    12    22    17 
# 2      B    15    29    17 
# 3      C    19    18    22 
# 4      D    19    12    25 
 
library(tidyr) 
 
#gather data from columns 2, 3, and 4 
gather(df, key="year", value="points", 2:4) 
 
#    player  year points 
# 1       A year1     12 
# 2       B year1     15 
# 3       C year1     19 
# 4       D year1     19 
# 5       A year2     22 
# 6       B year2     29 
# 7       C year2     18 
# 8       D year2     12 
# 9       A year3     17 
# 10      B year3     17 
# 11      C year3     22 
# 12      D year3     25 

本文参考链接:https://blog.csdn.net/neweastsun/article/details/121290472
阅读延展