根据 R 中的模式出现列表上的 Gsub
Gsub on a list according to pattern occurence in R
大家好,我有一个列表,例如:
my_list<-c("YP_0092.1_CP","C_scaffold_1017_18403-23022_-__Homo_sapiens","F_scaffold_1710_2458-7549_+__Pan_troglodys","ACPDHDDHH.2","X_scaffold_3287_5022-8483_-__Neandertalensis")
并且我想将列表中以 X
或 F
开头的每个元素的 "__"
更改为 "_"
然后我应该得到一个新列表:
c("YP_0092.1_CP","C_scaffold_1017_18403-23022_-__Homo_sapiens","F_scaffold_1710_2458-7549_+_Pan_troglodys","ACPDHDDHH.2","X_scaffold_3287_5022-8483_-_Neandertalensis")
有人有想法吗?
当然可以!
i <- grepl( "^(XF)", my_list )
my_list[i] <- gsub( "__", "_", my_list[i] )
大家好,我有一个列表,例如:
my_list<-c("YP_0092.1_CP","C_scaffold_1017_18403-23022_-__Homo_sapiens","F_scaffold_1710_2458-7549_+__Pan_troglodys","ACPDHDDHH.2","X_scaffold_3287_5022-8483_-__Neandertalensis")
并且我想将列表中以 X
或 F
"__"
更改为 "_"
然后我应该得到一个新列表:
c("YP_0092.1_CP","C_scaffold_1017_18403-23022_-__Homo_sapiens","F_scaffold_1710_2458-7549_+_Pan_troglodys","ACPDHDDHH.2","X_scaffold_3287_5022-8483_-_Neandertalensis")
有人有想法吗?
当然可以!
i <- grepl( "^(XF)", my_list )
my_list[i] <- gsub( "__", "_", my_list[i] )