R - 融合数据帧并将列中的值粘贴在一起



我有一个数据帧,dfregion,如下所示:

dput(dfregion)
structure(list(region = structure(c(1L, 2L, 3L, 3L, 1L), .Label = c("East", 
"New England", "Southeast"), class = "factor"), words = structure(c(4L, 
 2L, 1L, 3L, 5L), .Label = c("buildings, tallahassee", "center, mass, visitors", 
"god, instruct, estimated", "seeks, metropolis, convey", "teaching, academic, metropolis"
), class = "factor")), .Names = c("region", "words"), row.names = c(NA, 
-5L), class = "data.frame")
      region                       words                                                                                                                                             
 1        East                    seeks, metropolis, convey 
 3 New England                    center, mass, visitors 
 4   Southeast                    buildings, tallahassee
 5   Southeast                    god, instruct, estimated
 6        East                    teaching, academic, metropolis

我正在努力按区域"融化"或"重塑"此数据帧,然后想将这些单词粘贴在一起。

以下代码是我尝试过的:

dfregionnew<-dcast(dfregion, region ~ words,fun.aggregate= function(x) paste(x) )
dfregionnew<-dcast(dfregion, region ~ words, paste)
dfregionnew <- melt(dfregion,id=c("region"),variable_name="words")

最后,我做到了 - 但是我不确定这是实现我想要的最好方法

dfregionnew<-ddply(dfregion, .(region), mutate, index= paste0('words', 1:length(region)))
dfregionnew<-dcast(dfregionnew, region~ index, value.var ='words')

结果是以正确的方式重塑数据帧,但每个"单词"列都是独立的。随后,我尝试将这些列粘贴在一起,并在这样做时遇到各种错误。

dfregionnew$new<-lapply(dfregionnew[,2:ncol(dfregionnew)], paste, sep=",")
dfregionnew$new<-ldply(apply(dfregionnew, 1, function(x) data.frame(x = paste(x[2:ncol(dfregionnew], sep=",", collapse=NULL))))
dfregionnew$new <- apply( dfregionnew[ , 2:ncol(dfregionnew) ] , 1 , paste , sep = "," )

我能够通过执行以下操作来解决这个问题:

dfregionnew$new <- apply( dfregionnew[ , 2:5] , 1 , paste , collapse = "," )

我想我真正的问题是,是否可以使用 melt 或 dcast 一步完成此操作,而无需在输出后将各个列粘贴在一起。我对提高我的技能非常感兴趣,并希望在 R 中更快/更好的实践。提前感谢!

听起来

您只想将"word"列中的值粘贴在一起,在这种情况下,您应该能够按如下方式使用aggregate

aggregate(words ~ region, dfregion, paste)
#        region                                                     words
# 1        East seeks, metropolis, convey, teaching, academic, metropolis
# 2 New England                                    center, mass, visitors
# 3   Southeast          buildings, tallahassee, god, instruct, estimated

无需meltdcast


如果你确实想使用"reshape2"中的dcast,你可以尝试这样的东西:

dcast(dfregion, region ~ "WORDS", value.var="words", 
      fun.aggregate=function(x) paste(x, collapse = ", "))
#        region                                                     WORDS
# 1        East seeks, metropolis, convey, teaching, academic, metropolis
# 2 New England                                    center, mass, visitors
# 3   Southeast          buildings, tallahassee, god, instruct, estimated

最新更新