在空白处拆分字符串向量 [英] Split a string vector at whitespace
本文介绍了在空白处拆分字符串向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下向量:
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...
这篇关于在空白处拆分字符串向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文