粘贴混合向量的一些元素

Paste some elements of mixed vector

我有一个向量,其中的项可以后跟零个或多个以“/”开头的限定词。第一个元素应该始终是一个术语。

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

我想加入最后一项的限定词并获得一个新向量

Animals/physiology
Animals/metabolism*
Insects
Arabidopsis/immunology

通过 grepl 对以 / 开始的值 而不是 创建一个组标识符,拆分此组标识符,然后 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"

能想到比这更优雅的东西:

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)

另一种选择是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"