正则表达式 – 在第一个数字处拆分字符串
发布时间:2020-12-14 06:22:25 所属栏目:百科 来源:网络整理
导读:我想在最后一个字母和第一个数字之间拆分字符串: dat - read.table(text = " x y a1 0.1 a2 0.2 a3 0.3 a4 0.4 df1 0.1 df2 0.2 df13 0.3 df24 0.4 fcs111 0.1 fcs912 0.2 fcs113 0.3 fcsb8114 0.4",header=TRUE,stringsAsFactors=FALSE)desired.result - r
我想在最后一个字母和第一个数字之间拆分字符串:
dat <- read.table(text = " x y a1 0.1 a2 0.2 a3 0.3 a4 0.4 df1 0.1 df2 0.2 df13 0.3 df24 0.4 fcs111 0.1 fcs912 0.2 fcs113 0.3 fcsb8114 0.4",header=TRUE,stringsAsFactors=FALSE) desired.result <- read.table(text = " x1 x2 y a 1 0.1 a 2 0.2 a 3 0.3 a 4 0.4 df 1 0.1 df 2 0.2 df 13 0.3 df 24 0.4 fcs 111 0.1 fcs 912 0.2 fcs 113 0.3 fcsb 8114 0.4",stringsAsFactors=FALSE) StackOverflow上有很多类似的问题,但我找不到这种确切的情况.我知道这一定是个基本问题.如果我花了几个小时,我可能会想出来.抱歉.谢谢你的任何建议.我更喜欢基地R.如果这是重复我可以删除它.
您可以使用strsplit函数并为split参数提供正则表达式模式
cbind(dat,do.call(rbind,strsplit(dat$x,split = "(?<=[a-zA-Z])(?=[0-9])",perl = T))) ## x y 1 2 ## 1 a1 0.1 a 1 ## 2 a2 0.2 a 2 ## 3 a3 0.3 a 3 ## 4 a4 0.4 a 4 ## 5 df1 0.1 df 1 ## 6 df2 0.2 df 2 ## 7 df13 0.3 df 13 ## 8 df24 0.4 df 24 ## 9 fcs111 0.1 fcs 111 ## 10 fcs912 0.2 fcs 912 ## 11 fcs113 0.3 fcs 113 ## 12 fcsb8114 0.4 fcsb 8114 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |