dplyr mutate with qdap:synonmyms给出错误:结果大小错误

时间:2017-06-28 11:46:51

标签: r dplyr

这是一个包含2行的1列df:

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

我想在新的列syns中添加一个包含每个x的synonmyms的新字段:

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个返回的synomns,所以看起来我的代码试图为每个synomn添加一行,而我希望向量,在这种情况下有75个单词,在a列中随着崩溃添加到单行。 / p>

我该怎么做?

换句话说我想将整个chr向量添加到df

中的单个单元格

1 个答案:

答案 0 :(得分:2)

我们可以做到

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