paste - R: collapse a vector by two elements -
i have vector, e.g. sdata = c('a', 'b', 'c', 'd')
.
sdata [1] "a" "b" "c" "d"
how can collapse 2 (or more if needed) elements following output?
desiredoutput [1] "a b" "b c" "c d"
thanks!
edit: real sample data:
sdata = list(c("salmon", "flt", "atl", "farm", "wild", "per", "lb", "or", "fam", "pk"), c("vit", "min", "adult", "eye", "visit", "our", "pharmacy", "we", "accept", "express", "script", "offer", "val", "mathing", "pricing", "fast", "conv", "service"), c("ct", "gal", "or", "drawstring", "odor", "shield", "twist", "tie", "lawn", "leaf", "in", "plumber", "brush")) sdata [[1]] [1] "salmon" "flt" "atl" "farm" "wild" "per" "lb" "or" "fam" "pk" [[2]] [1] "vit" "min" "adult" "eye" "visit" "our" "pharmacy" "we" "accept" "express" "script" [12] "offer" "val" "mathing" "pricing" "fast" "conv" "service" [[3]] [1] "ct" "gal" "or" "drawstring" "odor" "shield" "twist" "tie" "lawn" [10] "leaf" "in" "plumber" "brush"
we can remove last , first element of 'sdata' , paste
vectors same length.
paste(sdata[-length(sdata)], sdata[-1]) #[1] "a b" "b c" "c d"
this can written as
paste(head(sdata,-1), tail(sdata,-1)) #[1] "a b" "b c" "c d"
update
based on new 'sdata' (in list
), use lapply
loop on list
elements , use same code
lapply(sdata, function(x) paste(head(x,-1), tail(x,-1))) #[[1]] #[1] "salmon flt" "flt atl" "atl farm" "farm wild" "wild per" #[6] "per lb" "lb or" "or fam" "fam pk" #[[2]] # [1] "vit min" "min adult" "adult eye" "eye visit" # [5] "visit our" "our pharmacy" "pharmacy we" "we accept" # [9] "accept express" "express script" "script offer" "offer val" #[13] "val mathing" "mathing pricing" "pricing fast" "fast conv" #[17] "conv service" #[[3]] # [1] "ct gal" "gal or" "or drawstring" "drawstring odor" # [5] "odor shield" "shield twist" "twist tie" "tie lawn" # [9] "lawn leaf" "leaf in" "in plumber" "plumber brush"
or without using anonymous function can paste
map
after removing first , last list
elements in sdata
map(paste, lapply(sdata, head, -1), lapply(sdata, tail, -1))
Comments
Post a Comment