[R] combine

Kaspar Pflugshaupt pflugshaupt at geobot.umnw.ethz.ch
Wed Dec 19 09:59:50 CET 2001

On 19.12.2001 9:26 Uhr, Nando Foppa wrote:

> I have several (the number of the files changes) textfiles. Each
> textfile contains one column with 3 rows (3 numerical values). I want to
> combine each file with another file and create new textfiles which then
> contain  2 columns with 3 rows. How can I do this in R? Do I have to
> use  cbind? How Do I use cbind for my problem?

Yes, use cbind() or data.frame(). You can e.g. do this (should work for most
well-behaved text files):

col1 <- scan("filename1")
col2 <- scan("filename2")
mydata <- data.frame(col1, col2)  # or the same with cbind(...)
write.table(mydata, file="filename3")

For more information on reading files into/from R, read the "R Data
Import/Export" manual which gets installed by default. See also
help(write.table), help(read.table), help(scan) for details on the functions
(such as specifying header rows, separators, N/A coding...).

> Instead of having several textfiles with one column and 3 rows I could
> use one textfile with n columns with 3 rows. I don`t know which way is
> better.

Depends on what you want to do with the data. For reading into R, both ways
work (for multi-column files, you'll want to use read.table() instead of




Kaspar Pflugshaupt
Geobotanisches Institut
Zuerichbergstr. 38
CH-8044 Zuerich

Tel. ++41 1 632 43 19
Fax  ++41 1 632 12 15

mailto:pflugshaupt at geobot.umnw.ethz.ch
privat:pflugshaupt at mails.ch

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list