2017-10-05 74 views
0

我正在使用R進行作業,我在下面有下面的代碼。向量B基本上是從向量a中的字符串中去除空白。然後,向量c將使用空格分割B中的字符串。然後,我要爲c列表排序。但我必須將其轉換爲其他類型的簡單原子向量。但是當我執行此操作時,出現下面的錯誤。R對錯誤的幫助

a <- c("Chpater 5 ", "Green Eggs and Ham", "Dr. Seuss ", "That Sam-I-am! ", "p. 25") 
a 
b<-str_trim (a, side = "both") 
b 

library (stringr) 

c<-str_split (b, pattern = " ") 
c 
sort (c) 
unlist (str_split (c, pattern = " ")) 

這是我從上面的代碼得到的結果。

[1] "c(\"Chpater\"," "\"5\")"   "c(\"Green\"," "\"Eggs\","  
[5] "\"and\","  "\"Ham\")"  "c(\"Dr.\","  "\"Seuss\")"  
[9] "c(\"That\"," "\"Sam-I-am!\")" "c(\"p.\","  "\"25\") 
sort (c) 

但是,我沒有得到變量c的排序,當我將c轉換爲簡單的原子向量。下面是我得到的消息,當我跑這條線

> unlist (str_split (c, pattern = " ")) 
Error in stri_split_regex(string, pattern, n = n, simplify = simplify, : 
argument `str` should be a character vector (or an object coercible to) 
> sort (c) 
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 
'x' must be atomic 
+0

這是因爲變量'C'在'str_split()'是大寫,而不是更低? – neilfws

+0

'c'和'C'不是同一個變量。嘗試使用更多的描述性變量名稱來提高清晰度,並且您將能夠看到問題。 – mikeck

+0

我讓c成爲小寫,但我仍然得到相同的錯誤。我添加了更多信息。 – cookiemonster

回答

2
## You need to unlist vector c and sort as below: 
sort(unlist(c)) 

#[1] "25"  "5"   "and"  "Chpater" "Dr."  "Eggs"  
#[7] "Green"  "Ham"  "p."  "Sam-I-am!" "Seuss"  "That" 
+0

非常感謝。它的工作 – cookiemonster

+1

很高興爲您提供幫助。 – Santosh