Split a string vector at whitespace
Solution 1
There's probably a better way, but here are two approaches with strsplit()
:
as.numeric(data.frame(strsplit(tmp3, " "))[2,])
as.numeric(lapply(strsplit(tmp3," "), function(x) x[2]))
The as.numeric() may not be necessary if you can use characters...
Solution 2
One could use read.table
on textConnection
:
X <- read.table(textConnection(tmp3))
then
> str(X)
'data.frame': 10 obs. of 2 variables:
$ V1: int 1500 1500 1510 1510 1520 1520 1530 1530 1540 1540
$ V2: int 2 1 2 1 2 1 2 1 2 1
so X$V2
is what you need.
Solution 3
It depends a little bit on how closely your actual data matches the example data you've given. I you're just trying to get everything after the space, you can use gsub
:
gsub(".+\\s+", "", tmp3)
[1] "2" "1" "2" "1" "2" "1" "2" "1" "2" "1"
If you're trying to implement a rule more complicated than "take everything after the space", you'll need a more complicated regular expresion.
Solution 4
What I think is the most elegant way to do this
> res <- sapply(strsplit(tmp3, " "), "[[", 2)
If you need it to be an integer
> storage.mode(res) <- "integer"
Solution 5
substr(x = tmp3, start = 6, stop = 6)
So long as your strings are always the same length, this should do the trick.
(And, of course, you don't have to specify the argument names - substr(tmp3, 6, 6)
works fine, too)
Zak
R aficionado, open source fan, trapped in a Microsoft environment
Updated on July 09, 2022Comments
-
Zak almost 2 years
I have the following vector:
tmp3 <- c("1500 2", "1500 1", "1510 2", "1510 1", "1520 2", "1520 1", "1530 2", "1530 1", "1540 2", "1540 1")
I would like to just retain the second number in each of the atoms of this vector, so it would read:
c(2,1,2,1,2,1,2,1,2,1)
-
Zak over 14 yearsThis is an elegant solution. Just what I was looking for. Thanks!
-
user295691 about 11 yearsAlso,
res <- as.numeric(sapply(...))
works as well;storage.mode
is a little scary -
pedrosaurio about 11 yearsI tried to use your solution but instead, using the column of a data frame and it didn't work straight away. I would add that for those cases you need to turn it into a list.
as.numeric(data.frame(strsplit(as.list(df$columnx), " "))[2,])
-
user3067923 over 7 yearscan you explain this…
-
Corey Levinson over 6 yearsthe function
gsub
is for replacing regex matches with something else. In this case, we use the regex.+\\s+
and replace any matches we find with the empty string""
. The regex translates to "Match anything in the beginning, but it has to end with a space" (the character space is written as\\s
)