我的载体为
dt <- c("1:7984985:A:G", "1:7984985-7984985:A:G", "1:7984985-7984985:T:G")
我想提取第二个:
之后的所有内容。
我想要的结果是
A:G , A:G, T:G
对此有什么解决方案?
答案 0 :(得分:1)
我们可以使用new_list = []
for i in range(0,14): #looping 14 times (or equivalent to size of dimension considered)
print (i)
print (data_in[i,:,:].shape) #Choosing a level makes it a 2D array
#(so, the shape at this stage will be (n,m))
new_list.append(data_in)
final_list = np.dstack(new_list)
#In case the final dimension gets added as the last dimension, do this:
a = np.empty((14,n,m))
final_list = np.transpose(a, (0,1,2))
print (final_list.shape)
来匹配一个或多个字符的两个实例,这些字符不是sub
(:
),后面是[^:]+
({{1 }})并替换为空白(:
^
也可以使用""
(如果它不是基于位置)完成
sub("^([^:]+:){2}", "", dt)
#[1] "A:G" "A:G" "T:G"
或使用trimws
中的trimws(dt, whitespace = "[-0-9:]")
#[1] "A:G" "A:G" "T:G"
str_remove
答案 1 :(得分:1)
您可以使用sub
,捕获要保留在捕获组(...)
中的项目,然后在sub
的替换参数中引用它们:
sub("^.:[^:]+:(.:.)", "\\1", dt, perl = T)
[1] "A:G" "A:G" "T:G"
或者,您可以使用str_extract
和(?<=...)
之后的正向查找:
library(stringr)
str_extract(dt, "(?<=:)[A-Z]:[A-Z]")
[1] "A:G" "A:G" "T:G"
答案 2 :(得分:0)
或者简单地使用str_split返回2个值的列表。 ´str_split(“ 1:7984985:A:G”,“ \:”,n = 3)[[1]] [3]´