2017-06-28 59 views
2

這裏有一個1列DF 2行:dplyr與qdap變異:synonmyms給錯誤:錯誤的結果大小

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

我想添加一個新的領域,在新列的SYN每個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列中的一行中並伴隨崩潰。

我該怎麼做?

換句話說我想一個DF內的整個CHR載體添加到單個細胞

+0

如果返回元素的數量比輸入的數量越多,則你可能需要做''''x%>%do(data.frame(syns =同義詞(。$ a,return.list = FALSE,report.null = TRUE)))' – akrun

+0

你好@akrun看起來怎麼樣?我的目標是以2列2行數據框結束,在這種情況下,x [1,2]將是包含75個字的字符向量 –

+0

如果是這種情況,請嘗試'x%>%rowwise() %>%mutate(syns = list(同義詞(a,return.list = F,report.null = T)))' – akrun

回答

2

我們可以做

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