[R] merge some columns
David Winsemius
dwinsemius at comcast.net
Fri Sep 2 15:42:51 CEST 2011
On Sep 2, 2011, at 9:30 AM, David Winsemius wrote:
>
> On Sep 2, 2011, at 8:34 AM, Joao Fadista wrote:
>
>> Dear all,
>>
>> I would like to know how to merge columns like:
>>
>> Input file:
>> V1 V2 V3 V4 V5 V6
>> 1 G A G G G G
>> 2 A A G A A G
>>
>
> Looked like an mapply-type problem:
>
> > with(dat,
> mapply(paste,
> list(V1, V3, V5),
> list(V2, V4, V6),
> MoreArgs=list(sep="/") )
> )
There is a further refinement that is possible that will result in
naming of the columns made possible by the behavior of the USE.NAMES
feature of mapply. From the help page: "use names if the first ...
argument has names, or if it is a character vector, use that character
vector as the names";
with(dat, mapply(paste,
list(V1 =V1, V2=V3, V3=V5),
list(V2, V4, V6),
MoreArgs=list(sep="/") ) )
V1 V2 V3
[1,] "G/A" "G/G" "G/G"
[2,] "A/A" "G/A" "A/G"
>
> [,1] [,2] [,3]
> [1,] "G/A" "G/G" "G/G"
> [2,] "A/A" "G/A" "A/G"
>
>
>> Desired output file:
>> V1 V2 V3
>> 1 G/A G/G G/G
>> 2 A/A G/A A/G
>>
>> So for every 2 consecutive columns merge their content into one.
>> Thanks in advance.
>>
>>
>> [[alternative HTML version deleted]]
> --
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list