Skip to main content

2.8) Exporting Data


Exporting a data frame or matrix from R is easy. There’s a function write.csv(), which produces files with commas between the values, suitable for reading into Excel. Or you can use the more general function write.table(), which uses spaces by default but can be customized with the argument sep=. Note that you always have to give a full file name, including the extension ".csv", ".txt", or whatever you want. Try exporting your modified version of GRASS by typing write.csv(GRASS, "GRASSmod.csv"). Go and find it and open it in Excel or another programme.

If you’re exchanging files with someone whose system uses commas for the decimal point and semicolons for separating values, the function write.csv2() does this – and to read in their CSV files, you might need read.csv(sep=";", dec=",").