将一行中的数据分成多行

时间:2019-05-23 14:37:05

标签: r reshape data-cleaning

我有一个代码,可以查看哪些人在某些小组中工作。当我要求每个小组的负责人介绍为他们工作的人时,在调查中,我得到了所有团队成员的一行。我需要的是使用其组信息将数据清除为多行。

我不知道从哪里开始。

这就是我的数据框的样子,

LeaderName <- c('John','Jane','Louis','Carl')

Group <- c('3','1','4','2')

Member1 <- c('Lucy','Stephanie','Chris','Leslie')

Member1ID <- c('1','2','3','4')

Member2 <- c('Earl','Carlos','Devon','Francis')

Member2ID <- c('5','6','7','8')

Member3 <- c('Luther','Peter','','Severus')

Member3ID <- c('9','10','','11')

GroupInfo <- data.frame(LeaderName, Group, Member1, Member1ID, Member2 ,Member2ID, Member3, Member3ID)

这就是我希望通过特定代码显示的内容

LeaderName_ <- c('John','Jane','Louis','Carl','John','Jane','Louis','Carl','John','Jane','','Carl')

Group_ <- c('3','1','4','2','3','1','4','2','3','1','','2')

Member <- c('Lucy','Stephanie','Chris','Leslie','Earl','Carlos','Devon','Francis','Luther','Peter','','Severus')

MemberID <- c('1','2','3','4','5','6','7','8','9','10','','11')

ActualGroupInfor <- data.frame(LeaderName_,Group_,Member,MemberID)

2 个答案:

答案 0 :(得分:1)

一个选项是melt中的data.table,并在patterns参数中指定列名measure

library(data.table)
melt(setDT(GroupInfo), measure = patterns("^Member\\d+$", 
    "^Member\\d+ID$"), value.name = c("Member", "MemberID"))[, variable := NULL][]
#    LeaderName Group    Member MemberID
# 1:       John     3      Lucy        1
# 2:       Jane     1 Stephanie        2
# 3:      Louis     4     Chris        3
# 4:       Carl     2    Leslie        4
# 5:       John     3      Earl        5
# 6:       Jane     1    Carlos        6
# 7:      Louis     4     Devon        7
# 8:       Carl     2   Francis        8
# 9:       John     3    Luther        9
#10:       Jane     1     Peter       10
#11:      Louis     4                   
#12:       Carl     2   Severus       11

答案 1 :(得分:1)

这是 base r 中的一种解决方案:

reshape(
 data=GroupInfo, 
 idvar=c("LeaderName", "Group"),
 varying=list(
  Member=which(names(GroupInfo) %in% grep("^Member[0-9]$",names(GroupInfo),value=TRUE)),
  MemberID=which(names(GroupInfo) %in% grep("^Member[0-9]ID",names(GroupInfo),value=TRUE))), 
 direction="long", 
 v.names = c("Member","MemberID"),
 sep="_")[,-3]
#>           LeaderName Group    Member MemberID
#> John.3.1        John     3      Lucy        1
#> Jane.1.1        Jane     1 Stephanie        2
#> Louis.4.1      Louis     4     Chris        3
#> Carl.2.1        Carl     2    Leslie        4
#> John.3.2        John     3      Earl        5
#> Jane.1.2        Jane     1    Carlos        6
#> Louis.4.2      Louis     4     Devon        7
#> Carl.2.2        Carl     2   Francis        8
#> John.3.3        John     3    Luther        9
#> Jane.1.3        Jane     1     Peter       10
#> Louis.4.3      Louis     4                   
#> Carl.2.3        Carl     2   Severus       11

reprex package(v0.2.1)于2019-05-23创建