根据作者将字符串粘贴在一起

时间:2014-10-22 23:58:06

标签: r string

我想在不同的列中基于值(paste(c(...), collapse=" "))在数据框中合并/粘贴(author)字符串。我正在寻找一种有效的方法。

df <- data.frame(author = c("Shakespeare", 
                            "Dante",
                            "Proust",
                            "Shakespeare", 
                            "Dante",
                            "Proust",
                            "Shakespeare"),
                 text = c("Put the wild waters in this roar, allay them",
                          "Ma tu perche' ritorni a tanta noia?",
                          "Longtemps, je me suis couché de bonne heure",
                          "The very virtue of compassion in thee",
                          "Pensa oramai qual fu colui che degno",
                          "Quelle horreur! me disais-je",
                          "She said thou wast my daughter; and thy father"))

最终结果应该是

result <- c("Put the wild waters in this roar, allay them The very virtue of compassion in thee She said thou wast my daughter; and thy father",
            "Ma tu perche' ritorni a tanta noia? Pensa oramai qual fu colui che degno",
            "Longtemps, je me suis couché de bonne heure Quelle horreur! me disais-je")
names(result) <- c("Shakespeare","Dante","Proust")
result
# Shakespeare 
# "Put the wild waters in this roar, allay them The very virtue of compassion in thee She said thou wast my daughter; and thy father" 
# Dante 
# "Ma tu perche' ritorni a tanta noia? Pensa oramai qual fu colui che degno" 
# Proust 
# "Longtemps, je me suis couché de bonne heure Quelle horreur! me disais-je" 

我想我应该以某种方式使用apply系列中的某些功能。像

这样的东西
apply( df[??? , 2 , paste , collapse = " " )

但我不知道如何传递条件,然后获取粘贴字符串对应的作者的名称...

1 个答案:

答案 0 :(得分:1)

tapply或多或少与预期完全一致:

tapply(df$text, df$author, paste, collapse = " ")

更多 en vogue 解决方案是使用dplyr

library(dplyr)
df %>% group_by(author) %>% summarize(passage = paste(text, collapse = " "))