在r中将一行变成多行 [英] Turning one row into multiple rows in r

查看:46
本文介绍了在r中将一行变成多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 R 中,我有数据,其中每个人都有多个会话日期,以及一些测试的分数,但这都在一行中.我想更改它,以便我有多个包含人员信息的行,但只有一个会话日期和相应的考试成绩,并且为每个人执行此操作.此外,每个人可能完成了不同数量的会话.

In R, I have data where each person has multiple session dates, and the scores on some tests, but this is all in one row. I would like to change it so I have multiple rows with the persons info, but only one of the session dates and corresponding test scores, and do this for every person. Also, each person may have completed different number of sessions.

例如:

ID  Name  Session1Date  Score  Score  Session2Date  Score  Score
23  sjfd  20150904      2      3      20150908      5      7
28  addf  20150905      3      4      20150910      6      8

致:

ID  Name  SessionDate  Score  Score
23  sjfd  20150904     2      3     
23  sjfd  20150908     5      7
28  addf  20150905     3      4
28  addf  20150910     6      8

推荐答案

您可以使用 data.table 开发版中的 melt ie.v1.9.5.它可以将多个度量"列作为列表.安装说明在这里

You can use melt from the devel version of data.table ie. v1.9.5. It can take multiple 'measure' columns as a list. Instructions to install are here

library(data.table)#v1.9.5+
melt(setDT(df1), measure = patterns("Date$", "Score(\\.2)*$", "Score\\.[13]"))
#   ID Name variable   value1 value2 value3
#1: 23 sjfd        1 20150904      2      3
#2: 28 addf        1 20150905      3      4
#3: 23 sjfd        2 20150908      5      7
#4: 28 addf        2 20150910      6      8

或者使用 base R 中的 reshape,我们可以将 direction 指定为 'long' 并将 variing 指定为列索引的list

Or using reshape from base R, we can specify the direction as 'long' and varying as a list of column index

 res <- reshape(df1, idvar=c('ID', 'Name'), varying=list(c(3,6), c(4,7), 
         c(5,8)), direction='long')
 res  
 #            ID Name time Session1Date Score Score.1
 #23.sjfd.1 23 sjfd    1     20150904     2       3
 #28.addf.1 28 addf    1     20150905     3       4
 #23.sjfd.2 23 sjfd    2     20150908     5       7
 #28.addf.2 28 addf    2     20150910     6       8

如果需要,可以更改rownames

 row.names(res) <- NULL 

更新

如果列遵循特定的顺序,即 3rd 与 6th、4th 与 7th、5th 与 8th 分组,我们可以创建列索引的 matrix 然后 split 到获取 reshapevariing 参数的 list.

Update

If the columns follow a specific order i.e. 3rd grouped with 6th, 4th with 7th, 5th with 8th, we can create a matrix of column index and then split to get the list for the varying argument in reshape.

 m1 <- matrix(3:8,ncol=2)
 lst <- split(m1, row(m1))
 reshape(df1, idvar=c('ID', 'Name'), varying=lst, direction='long')

这篇关于在r中将一行变成多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆