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
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)
Another option is scan()
. To get the second value, we can use a logical subset.
scan(text = tmp3)[c(FALSE, TRUE)]
# [1] 2 1 2 1 2 1 2 1 2 1
An easier way to split 1 column into 2 columns via data.table
require(data.table)
data_ex = data.table( a = paste( sample(1:3, size=10, replace=TRUE),"-separate", sep="" ))
data_ex[, number:= unlist( strsplit(x=a, split="-") )[[1]], by=a]
data_ex[, word:= unlist( strsplit(x=a, split="-") )[[2]], by=a ]