在R中使用Regex拆分列

时间:2016-05-19 14:36:41

标签: regex r dataframe

我有一个

类型的角色数据
a[1:5]  
"Tampa Bay Buccaneers / 1st / 1st pick / 2015" 
"Tennessee Titans / 1st / 2nd pick / 2015"      
"Oakland Raiders / 1st / 4th pick / 2015"      
"Washington Redskins / 1st / 5th pick / 2015"  
"New York Jets / 1st / 6th pick / 2015"

Q1。我想将该列拆分为四个单独的列并存储 数据框中的每一个.Eg:

Tampa Bay Buccaneers, Tennessee Titans ... in data frame1 , 
1st, 1st, ... in data frame2
1st pick, 2nd pick, ... in data frame 3
2015, 2015, ... in data frame 4

Q2。我们需要另一个数据框,其中包含数字,并且所有文本都在变量“a”中删除。 例如:112015,122015,132015等。

我被Regex困住了。请帮忙。

3 个答案:

答案 0 :(得分:1)

我们可以拆分' a'使用$DriveLetter = Get-Content 'letter.txt' & "${DriveLetter}:\Autorun.exe" " \\/ ")list进行矢量转换并转换为data.frame。

rbind

如果我们需要多个data.frames,as.data.frame(do.call(rbind, strsplit(a, " \\/ ")), stringsAsFactors=FALSE) transpose输出(' lst'),请转换list中的每个vectorlist,更改data.frame个名称并使用list创建多个' data.frame'全球环境中的对象(虽然不推荐)

list2env

数据

library(data.table)
lst <- transpose(strsplit(a, " \\/ "))
list2env(setNames(Map(function(x,y) setNames(data.frame(x),y) , lst, 
        "Col1"), paste0("df", seq_along(lst))), envir = .GlobalEnv)
 df1
#               Col1
#1 Tampa Bay Buccaneers
#2     Tennessee Titans
#3      Oakland Raiders
#4  Washington Redskins
#5        New York Jets

答案 1 :(得分:1)

a<-c("Tampa Bay Buccaneers / 1st / 1st pick / 2015", "Tennessee Titans / 1st / 2nd pick / 2015",
"Oakland Raiders / 1st / 4th pick / 2015" , "Washington Redskins / 1st / 5th pick / 2015" , "New York Jets / 1st / 6th pick / 2015")

library(stringr)
library(plyr)
options(stringsAsFactors = FALSE)

Q1<-do.call(rbind,str_split(a, "/"))
Q1b<-apply(Q1,2,data.frame)
Q2<-as.data.frame(gsub("[A-Za-z///]+[[:blank:]]","",a))

答案 2 :(得分:0)

stringr包有几个有用的函数:str_split和str_split_fixed。

library(stringr)
data.frame(str_split_fixed(a, "/", n=4))