Split vector of strings and paste subset of resulting elements into a new vector

前端 未结 4 957
悲&欢浪女
悲&欢浪女 2021-02-19 23:52

Define

z<- as.character(c(\"1_xx xx xxx_xxxx_12_sep.xls\",\"2_xx xx xxx_xxxx_15_aug.xls\"))

such that

> z
[1] \"1_xx xx x         


        
4条回答
  •  再見小時候
    2021-02-20 00:36

    You can do this using a combination of strsplit, substr and lapply:

    y <- strsplit(z,"_",fixed=TRUE)
    lapply(y,FUN=function(x){paste(x[1],x[4],substr(x[5],1,3),sep="_")})
    

提交回复
热议问题