2016-07-14 50 views
1

我有一個向量,其後可能跟有零個或多個以「/」開頭的限定符。第一個要素應該始終是一個術語。粘貼混合矢量的某些元素

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

我想參加預選賽的最後期限,並得到一個新的向量

Animals/physiology 
Animals/metabolism* 
Insects 
Arabidopsis/immunology 

回答

4

請通過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" 
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) 
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"