在空白处拆分字符串向量
问题描述:
我有以下向量:
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)
答
可能有更好的方法,但这里有两种使用 strsplit()
的方法:
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]))
如果您可以使用字符,则可能不需要 as.numeric()...
The as.numeric() may not be necessary if you can use characters...