r - dplyr 与 qdap 发生变异 :synonmyms giving Error: wrong result size

标签 r dplyr

这是一个 1 列 2 行的 df:

x <- data.frame(a = c("crash", "parking"))
> x
        a
1   crash
2 parking

我想添加一个新字段,其中包含新列 syns 中每个 x 的同义词:

library(dplyr)
library(qdap)
x <-  x %>%
  mutate(syns = synonyms(a, return.list = F, report.null = T))

给予

no match for the following:

parking
========================

Error: wrong result size (75), expected 2 or 1

如果我运行:

> synonyms("crash", return.list = F)

我得到:

 [1] "bang"                "boom"                "clang"               "clash"              
 [5] "clatter"             "clattering"          "din"                 "racket"             
 [9] "smash"               "smashing"            "thunder"             "break"              
[13] "break up"            "dash to pieces"      "disintegrate"        "fracture"           
[17] "fragment"            "shatter"             "shiver"              "splinter"           
[21] "dash"                "fall"                "fall headlong"       "give way"           
[25] "hurtle"              "lurch"               "overbalance"         "pitch"              
[29] "plunge"              "precipitate oneself" "sprawl"              "topple"             
[33] "bump (into)"         "collide"             "crash-land"          "drive into"         
[37] "have an accident"    "hit"                 "hurtle into"         "plough into"        
[41] "run together"        "wreck"               "accident"            "bump"               
[45] "collision"           "jar"                 "jolt"                "pile-up"            
[49] "prang"               "smash-up"            "thud"                "thump"              
[53] "bankruptcy"          "collapse"            "debacle"             "depression"         
[57] "downfall"            "failure"             "ruin"                "be ruined"          
[61] "fail"                "fold"                "fold up"             "go belly up"        
[65] "go broke"            "go bust"             "go to the wall"      "go under"           
[69] "emergency"           "immediate"           "intensive"           "round-the-clock"    
[73] "speeded-up"          "telescoped"          "urgent"    

我可以看到 75 个返回的同义词,所以看起来我的代码正在尝试为每个同义词添加一行,而我希望将向量(在本例中包含 75 个单词)添加到 a 列崩溃旁边的单行中。

我该怎么做?

换句话说,我想将整个 chr 向量添加到 df 中的单个单元格

最佳答案

我们能做到

x %>%
  rowwise() %>% 
  mutate(syns = list(synonyms(a, return.list = FALSE, report.null = TRUE)))

关于r - dplyr 与 qdap 发生变异 :synonmyms giving Error: wrong result size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44801522/

相关文章:

r - 没有对重复的两个向量的唯一组合

r - 在一列中迭代使用 cumsum()

r - 从整数向量构建一个更长的向量,包括与原始整数的距离最多为 10 的所有整数

r - 使用 Tidyr/Dplyr 汇总字符串组的计数

r - 在 dplyr 中取消引用 mutate 函数右侧的变量名

r - 按组汇总 wtd.quantile

r - 在自定义统计中转义 'discrete aesthetic implies group'

重新制定 R log(Y+1)

r - 如何按组计算事件之间的天数

r - 在 dplyr 中,是否可以使用 mutate 指定在何处添加新列?