如何根据第三个基于两个单元格的组合创建一个新表?

时间:2015-02-11 20:26:59

标签: r excel

我有一个名为"会议"第二个名为"会员"。每次会议都有几位成员参加此次活动。我想要做的是使用"成员"的二进制关系提取表格。谁参加同一次会议。

让我们说,我们有#19;联合国会议1945年"参与者John,Jack,Jamie和Pablo。该表必须给我:

John, Jack
John, Jamie
John, Pablo
Jack, Jamie
Jack, Pablo
Jamie, Pablo

1 个答案:

答案 0 :(得分:-1)

您可能没有在R中要求解决方案,但使用SPSS解决此问题会非常痛苦(如果可能的话)。

假设你有一个csv文件(Conference-Participants.csv),它看起来像这样:

Conference,Participant
UN Conference 1945,John
UN Conference 1945,Jack
UN Conference 1945,Jamie
UN Conference 1945,Pablo
UN Conference Kyoto 1997,Ben
UN Conference Kyoto 1997,Katie
UN Conference Kyoto 1997,Julie

在R中你可以通过以下方式解决你的难题。

library(data.table)
library(combinat) # you probably have to install this package first

# read csv-file
df <- read.csv("Conference-Participants.csv")

# split the data by conferences
subs <- split(df, df$Conference)

# create new data frame with "Conference" and all possible 
# pair combinations of participants ("Participant 1", "Participant 2")
df2 <- rbindlist(
  lapply(subs, 
     function(sub) as.data.frame(cbind(
       as.character(sub$Conference[1]), 
       t(combn(sub$Participant,2))
       ))
  )
)

# Set the column names
setnames(df2, c("Conference", "P1", "P2"))

# store the new data frame to a csv-file
write.csv(df2, "Conference-Pairs.csv")

结果然后看起来像这样

                 Conference    P1    P2
1:       UN Conference 1945  John  Jack
2:       UN Conference 1945  John Jamie
3:       UN Conference 1945  John Pablo
4:       UN Conference 1945  Jack Jamie
5:       UN Conference 1945  Jack Pablo
6:       UN Conference 1945 Jamie Pablo
7: UN Conference Kyoto 1997   Ben Katie
8: UN Conference Kyoto 1997   Ben Julie
9: UN Conference Kyoto 1997 Katie Julie