licata1996
licata1996

Reputation: 45

Splitting a string in R

Consider:

x<-strsplit("This is an example",split="\\s",fixed=FALSE)

I am surprised to see that x has length 1 rather than length 4:

> length(x)
[1] 1

like this, x[3] is null. But If I unlist, then:

> x<-unlist(x)
> x
[1] "This"    "is"      "an"      "example"
> length(x)
[1] 4 

only now x[3] is "an".

Why wasn't that list originally by length 4 so that elements can be accessed by indexing? This gives troubles to access the splitted elements, since I have to unlist first.

Upvotes: 1

Views: 67

Answers (2)

RHertel
RHertel

Reputation: 23788

That's because strsplit generates a list containing each element (word). Try

> x[[1]]
#[1] "This"    "is"      "an"      "example"

and

> length(x[[1]])
#[1] 4

Upvotes: 1

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

This allows strsplit to be vectorized for its input argument. For instance, it will allow you to split a vector such as:

x <- c("string one", "string two", "and string three")

into a list of split results.

You do not need to unlist, but rather, you can refer to the element by a combination of its list index and the vector index. For instance, if you wanted to get the second word in the second item, you can do:

> x <- c("string one", "string two", "and string three")
> y  <- strsplit(x, "\\s")
> y[[2]][2]
[1] "two"

Upvotes: 2

Related Questions