0

I have this : data1

       Aux     Cux
aa     3       3 
bbb    0       0
ccc    7.8     7.8
dddd   2.32    2.09
eee    5.68    5.45

and this, data2

       Aux     Cux
aa     6       6
bbb    0.3     0.4
ccc    7.5     2.9
dddd   2.09    1.48
eee    0.62    0.62

And I want this final data

          file1     |     file2      |
       Aux  |  Cux  |  Aux  |  Cux   |
aa     3    |  3    |  6    |  6     |
bbb    0    |  0    |  0.3  |  0.4   |
ccc    7.8  |  7.8  |  7.5  |  2.9   |
dddd   2.32 |  2.09 |  2.09 |  1.48  |
eee    5.68 |  5.45 |  0.62 |  0.62  |

I think, csv file will be saved like this

,file1,,file2,,
,Aux,Cux,Aux,Cux
aa,3,3,6,6
bbb,0,0,0.3,0.4
ccc,7.8,7.8,7.5,2.9
dddd,2.32,2.09,2.09,1.48
eee,5.68,5.45,0.62,0.62

how can I do it ? thank you

5
  • Have you tried cbind or merge? Commented Jul 25, 2013 at 8:50
  • -1 it is basics of R, try some manuals. What have you tried so far? Commented Jul 25, 2013 at 8:54
  • Use write.table and pay attention to its append parameter. Commented Jul 25, 2013 at 8:54
  • How to give name to two columns ? and give another name to each of those two columns ? Commented Jul 25, 2013 at 9:00
  • You really have to show us what you've attempted. This is all really basic stuff, so it's unclear if you just haven't read the manual or you've tried something (which you haven't shown) and it's not working for you. Commented Jul 25, 2013 at 9:01

2 Answers 2

2

The data frames:

data1 <- read.table(text = "Aux     Cux
aa     3       3 
bbb    0       0
ccc    7.8     7.8
dddd   2.32    2.09
eee    5.68    5.45", header = TRUE, row.names = 1)

data2 <- read.table(text = "Aux     Cux
aa     6       6
bbb    0.3     0.4
ccc    7.5     2.9
dddd   2.09    1.48
eee    0.62    0.62", header = TRUE, row.names = 1)

Create a character matrix:

dat <- as.matrix(rbind(c(names(data1), names(data2)),
                       cbind(data1, data2)))

Set row and column names:

dimnames(dat) <- list(c("", rownames(data1)), c("file1", "", "file2", ""))

Write the table:

write.csv(dat, file = "filename.csv", quote = FALSE)

The resulting file:

,file1,,file2,
,Aux,Cux,Aux,Cux
aa,3,3,6,6
bbb,0,0,0.3,0.4
ccc,7.8,7.8,7.5,2.9
dddd,2.32,2.09,2.09,1.48
eee,5.68,5.45,0.62,0.62
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

data3 = cbind(data1, data2)
header = matrix(c('file1','','file2',''), nrow=1)
write.table(header, "output.csv", sep=',', quote=F, col.names=F, row.names=F)
write.table(data3, "output.csv", append = T, row.names=F)

You may just need to play around with row.names to get that right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.