Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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,我想提取第三个字母和后面的数字。下面是我想要的输出 df Chromosome aaChange 1 16 p.E548fs 2 16 p.S64X 3 16 p.P23H 4 16 p.G18V 5 16 p.L251S 谢谢。您可以使用base R中的sub执行此操作: Chromosome aaChange Protein_position 1 16 p.

我想提取第三个字母和后面的数字。下面是我想要的输出

df
   Chromosome aaChange
1          16 p.E548fs
2          16   p.S64X
3          16   p.P23H
4          16   p.G18V
5          16  p.L251S

谢谢。

您可以使用base R中的
sub
执行此操作:

   Chromosome aaChange Protein_position
 1         16 p.E548fs             E548
 2         16   p.S64X              S64
 3         16   p.P23H              P23
 4         16   p.G18V              G18
 5         16  p.L251S             L251
数据

transform(df, Protein_position = sub('..(.\\d+).*', '\\1', aaChange))

#  Chromosome aaChange Protein_position
#1         16 p.E548fs             E548
#2         16   p.S64X              S64
#3         16   p.P23H              P23
#4         16   p.G18V              G18
#5         16  p.L251S             L251

df您可以使用基本R中的
sub
执行此操作:

   Chromosome aaChange Protein_position
 1         16 p.E548fs             E548
 2         16   p.S64X              S64
 3         16   p.P23H              P23
 4         16   p.G18V              G18
 5         16  p.L251S             L251
数据

transform(df, Protein_position = sub('..(.\\d+).*', '\\1', aaChange))

#  Chromosome aaChange Protein_position
#1         16 p.E548fs             E548
#2         16   p.S64X              S64
#3         16   p.P23H              P23
#4         16   p.G18V              G18
#5         16  p.L251S             L251

df带
tidyverse

df <- structure(list(Chromosome = c(16L, 16L, 16L, 16L, 16L), 
aaChange = c("p.E548fs", "p.S64X", "p.P23H", "p.G18V", "p.L251S")), 
class = "data.frame", row.names = c(NA, -5L))
-输出

library(dplyr)
library(stringr)
df %>%
   mutate(Protein_position = str_replace(aaChange,
      '^[^.]+\\.(.*)[^0-9]+$', '\\1'))
数据
df带
tidyverse

df <- structure(list(Chromosome = c(16L, 16L, 16L, 16L, 16L), 
aaChange = c("p.E548fs", "p.S64X", "p.P23H", "p.G18V", "p.L251S")), 
class = "data.frame", row.names = c(NA, -5L))
-输出

library(dplyr)
library(stringr)
df %>%
   mutate(Protein_position = str_replace(aaChange,
      '^[^.]+\\.(.*)[^0-9]+$', '\\1'))
数据
df您想要匹配的模式似乎非常简单:它总是以大写字母开头,紧接着是一系列一个或多个数字。这给出了模式
[A-Z]\\d+
。我们可以将其输入到
stru extract

df <- structure(list(Chromosome = c(16L, 16L, 16L, 16L, 16L), 
aaChange = c("p.E548fs", "p.S64X", "p.P23H", "p.G18V", "p.L251S")), 
class = "data.frame", row.names = c(NA, -5L))
库(stringr)

df$Protein_position您想要匹配的模式似乎非常简单:它总是以大写字母开头,紧接着是一系列一个或多个数字。这给出了模式
[A-Z]\\d+
。我们可以将其输入到
stru extract

df <- structure(list(Chromosome = c(16L, 16L, 16L, 16L, 16L), 
aaChange = c("p.E548fs", "p.S64X", "p.P23H", "p.G18V", "p.L251S")), 
class = "data.frame", row.names = c(NA, -5L))
库(stringr)

df$Protein_位置谢谢。这对我很有效。如何理解此处的“\\1”?
\\1
用于以模式返回捕获的表达式(
(..)
)。这里我们将捕获第三个字符,后面是数字。谢谢。这对我很有效。如何理解此处的“\\1”?
\\1
用于以模式返回捕获的表达式(
(..)
)。这里我们要捕捉第三个字符,后面跟数字。