Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
减少并拆分为R中的几列_R_Regex - Fatal编程技术网

减少并拆分为R中的几列

减少并拆分为R中的几列,r,regex,R,Regex,我的氨基酸突变数据有一个问题。比如说, p.K303R p.? p.R1450* 我想要这样的退出 AA_mutation wt_residue position mt_residue p.K303R K 303 R p.? p.R1450* R 1450 * 我想从数据中删除“p.”、“?”,并将其拆分为三个新变量。我在excel上管理过,但在R平台上没有。 有人

我的氨基酸突变数据有一个问题。比如说,

p.K303R   
p.?
p.R1450*
我想要这样的退出

AA_mutation wt_residue  position    mt_residue
p.K303R       K          303           R
p.?         
p.R1450*      R           1450         *
我想从数据中删除“p.”、“?”,并将其拆分为三个新变量。我在excel上管理过,但在R平台上没有。 有人能在R上帮我吗。 问候
Fakhrul

使用
dplyr
extract
(have),我们可以使用:

library(dplyr)
df <- data.frame(AA_mutation = c("p.K303R", "p.?", "p.R1450*"))
df <- df %>%
  extract(AA_mutation, 
          into = c("wt_residue", "position", "mt_residue"), 
          regex = "p\\.([A-Z])?(\\d+)?([A-Z*])?",
          remove = FALSE)
df
库(dplyr)

df这里有一个带有
base R

lst <- regmatches(df1[[1]], gregexpr("([a-z]+)|([A-Z*]+)|[0-9]+", df1[[1]], perl = TRUE))
res <- do.call(rbind.data.frame, lapply(lst, `length<-`, max(lengths(lst))))
names(res) <- c("AA_mutation", "wt_residue",  "position",    "mt_residue")
res
#  AA_mutation wt_residue position mt_residue
#1           p          K      303          R
#2           p       <NA>     <NA>       <NA>
#3           p          R     1450          *
lst
lst <- regmatches(df1[[1]], gregexpr("([a-z]+)|([A-Z*]+)|[0-9]+", df1[[1]], perl = TRUE))
res <- do.call(rbind.data.frame, lapply(lst, `length<-`, max(lengths(lst))))
names(res) <- c("AA_mutation", "wt_residue",  "position",    "mt_residue")
res
#  AA_mutation wt_residue position mt_residue
#1           p          K      303          R
#2           p       <NA>     <NA>       <NA>
#3           p          R     1450          *