r - 熔化数据框并将列中的值粘贴在一起

标签 r dataframe plyr reshape2

我有一个数据框 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

我正在按区域“融化”或“ reshape ”此数据框,然后想将这些词粘贴在一起。

下面的代码是我试过的:

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')

结果是一个以正确方式 reshape 的数据框,但每个“单词”列都是分开的。 随后,我试图将这些列粘贴在一起,但在这样做时遇到了各种错误。

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

无需melting 或dcasting ....


如果您确实想使用来自“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

关于r - 熔化数据框并将列中的值粘贴在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20764671/

相关文章:

python - 将 dict 的 pandas dataframe 列扩展为 dataframe 列

python - 根据Python中groupby的第一个和最后一个值的条件创建一个新列

python - 在导出到 Excel 之前如何命名 DataFrame 中的行和列?

r - 如何将 POSIX 日期转换为一年中的某一天?

r - 使用 call() 赋值

r - 在大型数据框中生成指标

r - R:将数据框的每一行转换为一个列表项

r - 基于R中的列聚合字符串,仅保留第一个/最后一个

重复一条记录N次,创建一个从1到N的新序列

r - AzureCognitive R 包翻译服务返回 http 400 : "body of request is not valid JSON."