[R] Merge 2 columns into 1 column

arun smartpink111 at yahoo.com
Sun Jan 20 03:42:39 CET 2013



HI,

Not clear what you are trying to do:

set.seed(25)
df1<-as.data.frame(matrix(sample(1:40,20,replace=TRUE),ncol=4))
set.seed(15)
df2<-as.data.frame(matrix(sample(1:40,20,replace=TRUE),ncol=4))
 do.call(rbind,lapply(list(df1,df2),`[`,c(2,4)))
#   V2 V4
#1  40  6
#2  26 22
#3  14 30
#4   3 20
#5  12 30
#6  40 34
#7  33 18
#8  11 39
#9  28  6
#10 34 32

do.call(rbind,lapply(list(df1,df2),`[`,c(1,3)))
#in reading from read.csv()
do.call(rbind,lapply(lapply(files,read.csv),`[`,c(1,3)))
A.K.



----- Original Message -----
From: "qt2hot4u at gmail.com" <qt2hot4u at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Saturday, January 19, 2013 11:08 AM
Subject: [R] Merge 2 columns into 1 column

Hey everyone,

I just can’t wrap my head around this problem: I got 2 Columns with 4 columns. They basically look like this:
1 2 3 4
… … … …

I want to merge the two CSVs into a two-column list, which should look like this:
1 & 3 2 & 4
… …
… …

This is what I came up with so far:
files <- paste(list.files(files.dir, pattern = "*.csv"), sep = "")# Scan
mergedfiles <- do.call("rbind", lapply(files, read.csv)) # Merge

Obviously this will only merge the two CSVs into a four column list. Is there any way to merge columns 1 & 3 and columns 2 & 4 of both files into a single list?

Thanks and all the best
Kenji



    [[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.




More information about the R-help mailing list