2016-03-02 21 views

回答

3

我們可以嘗試

library(stringr) 
do.call(rbind, 
    lapply(str_extract_all(df1$Col1, 
     "(?<=\\()[0-9.]+|[0-9.]+(?=\\scm)"), as.numeric)) 
#  [,1] [,2] 
#[1,] 104.8 74.6 
#[2,] 216.0 78.8 
#[3,] 200.0 200.0 

如果我們需要使用dplyr

library(dplyr) 
library(purrr) 
str_extract_all(df1$Col1, "(?<=\\()[0-9.]+|[0-9.]+(?=\\scm)") %>% 
     map(~as.numeric(.)) %>% 
     do.call(rbind,.) 
# [,1] [,2] 
#[1,] 104.8 74.6 
#[2,] 216.0 78.8 
#[3,] 200.0 200.0 

或者與extracttidyr

library(tidyr) 
extract(df1, Col1, into=c("Col1", "Col2"), 
      "^[^(]+\\(([0-9.]+)\\D+([0-9.]+).*") 
# Col1 Col2 
#1 104.8 74.6 
#2 216 78.8 
#3 200 200 
+1

感謝您對不同的方式。這非常有幫助。 – MartinW

相關問題