【问题标题】:R dataframe combining rows based on columnsR数据框根据列组合行
【发布时间】:2016-12-09 13:36:21
【问题描述】:

我有以下数据框,其中包含我想要处理的名称和城市的定义,我不知道如何解释它,所以我在下面的表格中包含了输入和输出。

输入:

+---------+-----------+------------+---------------+
| Varname | Component |   names    |    cities     |
+---------+-----------+------------+---------------+
| A       | B         | Jack,Bruce | New york      |
| B       |           | Cathy      | Boston,Miami  |
| C       |           | Bob        | New york      |
| D       | C         | Dick,Nancy | Austin,Dallas |
| E       | A,C       |            |               |
| F       |           | Mandy      | Manchester    |
+---------+-----------+------------+---------------+

输出:

+---------+-----------+----------------------+------------------------+
| Varname | Component |        names         |         cities         |
+---------+-----------+----------------------+------------------------+
| A       |           | Jack,Bruce,Cathy     | New york,Boston,Miami  |
| B       |           | Cathy                | Boston,Miami           |
| C       |           | Bob                  | New york               |
| D       |           | Dick,Nancy,Bob       | Austin,Dallas,New york |
| E       |           | Jack,Bruce,Cathy,Bob | New york,Boston,Miami  |
| F       |           | Mandy                | Manchester             |
+---------+-----------+----------------------+------------------------+

正如您所希望看到的,我想获取组件列,对于该列中的每个 Varname,查找名称和城市(实际上我有更多的列)并将它们组合起来,这样我就有了一个完整的表。这可能吗?我不知道从哪里开始。我的表并不大,因此可以应用 for(){} 语句。

->编辑,我可能没有给出正确的示例,我已将输入替换为与我的数据更一致的内容。

输入的dput()

结构(列表(变量名 = 结构(1:6,.Label = c(“A”,“B”,“C”, “D”,“E”,“F”),class= “因子”),组件 = 结构(c(3L,1L, 1L, 4L, 2L, 1L), .Label = c("", "A,C", "B", "C"), class= "因子"), 名称=结构(c(5L,3L,2L,4L,1L,6L),.Label = c(“”, “鲍勃”,“凯茜”,“迪克,南希”,“杰克,布鲁斯”,“曼迪”),class=“因素”), 城市=结构(c(5L,3L,5L,2L,1L,4L),.Label = c(“”, “奥斯汀,达拉斯”,“波士顿,迈阿密”,“曼彻斯特”,“纽约” ), class= "因子")), .Names = c("Varname", "Component", "names", "cities"), class= "data.frame", row.names = c(NA, -6L ))

【问题讨论】:

  • 使用dput 提供您的数据示例,以便可以复制
  • 我包含了一个 dput()

标签: r dataframe


【解决方案1】:

不是最吸引人的一段 R 代码(也绝对不是最有效的),但它可以完成工作。希望其他人可以改进它。

starting_df <- read.table(text="Varname|Component|names|cities     
A||Jack,Bruce|New york
B||Cathy|Boston,Miami
C|A|Bob|New york
D|C|Dick,Nancy|Austin,Dallas",header=T, sep="|", stringsAsFactors=F)

##Grab all the rows whose Component values are in the Varname column and vice-versa
intermediate_df <- starting_df[(starting_df$Varname %in% starting_df$Component | starting_df$Component %in% starting_df$Varname ),]

##Change the rows in the names and cities columns to match your desired output (sorry about the for loop)
for (x in 1:nrow(intermediate_df)) {
  if (x == 1) {
    intermediate_df[x,'names'] <- intermediate_df$names[x]
    intermediate_df[x,'cities'] <- intermediate_df$cities[x]
  } else {
    intermediate_df[x,'names'] <- paste0(unique(unlist(strsplit(paste(intermediate_df$names[x-1],intermediate_df$names[x],sep = ","),split=","))),collapse=",")
    intermediate_df[x,'cities'] <- paste0(unique(unlist(strsplit(paste(intermediate_df$cities[x-1],intermediate_df$cities[x],sep = ","),split=","))),collapse=",")
  }
}

##Binding the new dataset with the starting dataset (but only Varnames that are in the new dataset)
final_df <- rbind(intermediate_df,starting_df[!(starting_df$Varname %in% intermediate_df$Varname),])

##Order by the Varname column to get the desired output
final_df <- final_df[order(final_df$Varname),]

你想要的输出:

 Varname Component names                     cities                
 A                 Jack,Bruce                New york              
 B                 Cathy                     Boston,Miami          
 C       A         Jack,Bruce,Bob            New york              
 D       C         Jack,Bruce,Bob,Dick,Nancy New york,Austin,Dallas

编辑新数据集:

这个使用了相当多的for loops(我在R中根本不喜欢做的事情),但它似乎产生了一些东西:

##Setting up the new dataset
starting_df1 <- structure(list(Varname = structure(1:6, .Label = c("A", "B", "C", "D", "E", "F"), class = "factor"), 
                              Component = structure(c(3L, 1L, 1L, 4L, 2L, 1L), .Label = c("", "A,C", "B", "C"), class = "factor"), 
                              names = structure(c(5L, 3L, 2L, 4L, 1L, 6L), .Label = c("", "Bob", "Cathy", "Dick,Nancy", "Jack,Bruce", "Mandy"), class = "factor"), 
                              cities = structure(c(5L, 3L, 5L, 2L, 1L, 4L), .Label = c("", "Austin,Dallas", "Boston,Miami", "Manchester", "New york" ), class = "factor")), 
                         .Names = c("Varname", "Component", "names", "cities"), class = "data.frame", row.names = c(NA, -6L ))

##Change the fields from factor variables to characters (so that you can use them for concatenating)
starting_df1 <- data.frame(apply(starting_df1, 2, FUN = function(x) {
  as.character(x)
}), stringsAsFactors = F)

##Nested for loops: For every row that has a value for the Component column, find its matches (and their indices) in the Varname column
##Then for the combination of indices to change the values you wish to change through concatenation operations for both the names and cities columns
for (i in which(!nchar(starting_df1$Component)==0)) {
  holder <- which(grepl(paste0(unlist(strsplit(starting_df1$Component[i],split=",")),collapse="|"),starting_df1$Varname))
  for (j in holder) {
    if (nchar(starting_df1$names[i])!=0) {
      starting_df1[i, "names"] <- paste0(unique(unlist(strsplit(paste(starting_df1$names[i],starting_df1$names[j],sep = ","),split=","))),collapse=",")
      starting_df1[i, "cities"] <- paste0(unique(unlist(strsplit(paste(starting_df1$cities[i],starting_df1$cities[j],sep = ","),split=","))),collapse=",")
    } else {
      starting_df1[i, "names"] <- starting_df1$names[j]
      starting_df1[i, "cities"] <- starting_df1$cities[j]
    }
  }
}

print(starting_df1, row.names = F, right = F)

期望的输出:

 Varname Component names                cities                
 A       B         Jack,Bruce,Cathy     New york,Boston,Miami 
 B                 Cathy                Boston,Miami          
 C                 Bob                  New york              
 D       C         Dick,Nancy,Bob       Austin,Dallas,New york
 E       A,C       Jack,Bruce,Cathy,Bob New york,Boston,Miami 
 F                 Mandy                Manchester            

【讨论】:

  • 谢谢!这是正确的,但对我的现实问题并没有真正的帮助。我在上面包含了一个更复杂的示例。我会玩弄你的代码,看看它是否对我有进一步的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2020-12-26
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 2022-10-05
  • 1970-01-01
相关资源
最近更新 更多