cookiemonster
cookiemonster

Reputation: 368

R help on error

I am working on a homework using R and I have this code below . Vector B is basically removing the white spaces from the strings in vector a. Vector c will then split the strings in B using the white spaces. Then Iwant to sort the list for c. but I have to convert it to simple atomic vector in other to sort. but when I do theat I get an error stated below.

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 = " "))

This is the result I get from the last code above.

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

However I am not getting the sort for variable c when I convert c to a simple atomic vector. Below is the message I get when I run that line

> 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 

Upvotes: 0

Views: 218

Answers (1)

Santosh M.
Santosh M.

Reputation: 2454

## 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"   

Upvotes: 2

Related Questions