[R] split list of characters in groups of 2

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Nov 17 11:03:43 CET 2011



On 17.11.2011 10:31, Dennis Murphy wrote:
> Hi:
>
> Here's one way:
>
> apply(matrix(var.names, ncol = 2, byrow = TRUE), 1, function(x)
> paste(x[1], x[2], sep = ','))
> [1] "a,b" "c,d" "e,f"

Or for short and slightly faster for huge data use column-wise 
operations as in:

apply(matrix(var.names, nrow=2), 2, paste, collapse=", ")

Best,
Uwe Ligges



> HTH,
> Dennis
>
> On Wed, Nov 16, 2011 at 9:46 PM, B77S<bps0002 at auburn.edu>  wrote:
>> hi,
>>
>> If i have a list of things, like this
>>
>> var.names<- c("a", "b", "c", "d", "e", "f")
>>
>> how can i get this:
>>
>> "a, b", "c, d", "e, f"
>>
>> thanks ahead of time.
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/split-list-of-characters-in-groups-of-2-tp4079031p4079031.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list