Fred Fred - 1 year ago 81
R Question

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


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 xxx_xxxx_12_sep.xls" "2_xx xx xxx_xxxx_15_aug.xls"

I want to create a vector w such that

> w
[1] "1_12_sep" "2_15_aug"

That is, split each element of z by _ and then join elements 1,4,5, with the .xls removed from the latter.

I can manage the split part, but not sure what function to provide, e.g something like"

w <- as.character(lapply(strsplit(z,"_"), function(x) ???))

Answer Source

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

y <- strsplit(z,"_",fixed=TRUE)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download