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 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]]
Hi: Here's one approach: d <- read.table(textConnection(" V1 V2 V3 V4 V5 V6 1 G A G G G G 2 A A G A A G"), header = TRUE, stringsAsFactors = FALSE) closeAllConnections() # Create two vectors of variable names, one for odd numbered, # one for even numbered vars1 <- names(d)[seq_along(names(d)) %% 2 == 1] vars2 <- names(d)[seq_along(names(d)) %% 2 == 0] # Apply the paste sequentially to corresponding pairs # in vars1 and vars2; get() is used to get the data associated # with the variable names in vars1 and vars2 d2 <- sapply(seq_along(vars1), function(j) with(d, paste(get(vars1[j]), get(vars2[j]), sep = '/'))) # Convert to data frame: d2 <- as.data.frame(d2, stringsAsFactors = FALSE) str(d2) HTH, Dennis On Fri, Sep 2, 2011 at 5:34 AM, Joao Fadista <Joao.Fadista at med.lu.se> 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 > > 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]] > > ______________________________________________ > 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. >
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="/") ) ) [,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
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