粘贴混合矢量的一些元素

时间:2016-07-14 01:15:51

标签: r ncbi

我有一个带有术语的向量,后面跟着以“/”开头的零个或多个限定符。第一个元素应该始终是一个术语。

 mesh <- c("Animals", "/physiology" , "/metabolism*", 
           "Insects", "Arabidopsis", "/immunology" )

我想加入限定符和最后一个术语并获得一个新的向量

Animals/physiology
Animals/metabolism*
Insects
Arabidopsis/immunology

3 个答案:

答案 0 :(得分:4)

通过grepl为{em> not 创建一个组标识符,以/开头,拆分此组标识符,然后paste0

unlist(by(mesh, cumsum(grepl("^[^/]",mesh)), FUN=function(x) paste0(x[1], x[-1])))
#                      11                       12                        2                        3 
#    "Animals/physiology"    "Animals/metabolism*"                "Insects" "Arabidopsis/immunology"

答案 1 :(得分:1)

另一个选项是tapply

 unlist(tapply(mesh, cumsum(grepl("^[^/]", mesh)), 
           FUN = function(x) paste0(x[1], x[-1])), use.names=FALSE)
 #[1] "Animals/physiology"     "Animals/metabolism*"    "Insects"                "Arabidopsis/immunology"

答案 2 :(得分:0)

可以想到比这更优雅的东西:

mesh <- c("Animals", "/physiology" , "/metabolism*", 
       "Insects", "Arabidopsis", "/immunology" )

#gets "prefixes", assuming they all start with a letter:
pre <- grep(pattern = "^[[:alpha:]]", x = mesh) 

#gives integer IDs for the prefix-suffix groupings
id <- rep(1:length(pre), times = diff(c(pre,length(mesh) + 1)))

#function that pastes the first term in vector to any remaining ones 
     #will just return first term if there are no others
combine <- function(x) paste0(x[1], x[-1])

#groups mesh by id, then applies combine to each group
results <- tapply(mesh, INDEX = id, FUN = combine)

unlist(results)