cosmos1990 - 3 years ago 164

R Question

In the following example

`x = strsplit('30 min', ' ')`

The return value as the docs state is

A list of the same length as x, the i-th element of which contains the vector of splits of x[i].

I would expect

`x[[1]]`

`30`

`x[[2]]`

`min`

However

`x[[1]][1]`

`30`

`x[[1]][2]`

`min`

What is the explanation for this?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

The explanation is that `strsplit`

expects a vector of input strings, each of which will be split into an array of strings, which are returned in the form of a list.
If you only provide this one string, it will be treated like it was the single entry of a vector. Thus, the result is a list with one entry (`x[[1]]`

) and it's split contents (`x[[1]][1]`

and `x[[1]][2]`

), as you've described.

Just use `x = unlist(strsplit('30 min', ' '))`

and you'll get a character-vector, where `x[1]`

returns `30`

and `x[2]`

returns `min`

.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**