Split a string vector at whitespace

后端 未结 9 1932
独厮守ぢ
独厮守ぢ 2020-12-08 09:35

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         


        
9条回答
  •  醉梦人生
    2020-12-08 10:30

    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...

提交回复
热议问题