Search code examples
rmergepasten-gram

R: collapse a vector by two elements


I have a vector, e.g. sdata = c('a', 'b', 'c', 'd').

sdata
[1] "a" "b" "c" "d"

How can I collapse it by two (or more if needed) elements to get the 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"     

Solution

  • We can remove the last and first element of 'sdata' and paste the vectors with the same length.

     paste(sdata[-length(sdata)], sdata[-1])
     #[1] "a b" "b c" "c d"
    

    This can be also written as

     paste(head(sdata,-1), tail(sdata,-1))
     #[1] "a b" "b c" "c d"
    

    Update

    Based on the new 'sdata' (in a list), we use lapply to loop over the list elements and use the 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 we can paste with Map after removing the first and last list elements in the sdata

      Map(paste, lapply(sdata, head, -1), lapply(sdata, tail, -1))