[R] Migrating from S-Plus to R - Exporting Tables

Gustaf Rydevik gustaf.rydevik at gmail.com
Thu Jul 3 10:00:32 CEST 2008


On Thu, Jul 3, 2008 at 2:17 AM, jim holtman <jholtman at gmail.com> wrote:
> Does something like this get you close:
>
> x <- list()
> keys <- LETTERS[1:6]
> # create
> for (i in keys){
>    x[[i]] <- data.frame(a=1:5, b=1:5, c=1:5)
> }
> # output
> output <- file('tempxx.txt', 'w')
> for (i in keys){
>    write.table(i, row.names=FALSE, col.names=FALSE, file=output, quote=FALSE)
>    write.table(x[[i]], file=output, quote=FALSE)
> }
> close(output)
>


In order to get "row.names" written above the row names, I think you
have to cheat a bit:
(modifying Jim's code)

x <- list()
keys <- LETTERS[1:6]
# create
for (i in keys){
   x[[i]] <- data.frame(a=1:5, b=1:5, c=1:5)
}
# output
output <- file('tempxx.txt', 'w')
for (i in keys){
   write.table(i, row.names=FALSE, col.names=FALSE, file=output, quote=FALSE)
   write.table(data.frame(RowNames=row.names(x[[i]]),x[[i]]),
file=output,   quote=FALSE,row.names=FALSE) ##excluding actual
rownames, adding them as a column.
}
close(output)

-----
It seems as if you can't get it to write "row.names", since that is a
restricted name in a dataframe, but hopefully "RowNames" is good
enough.

/Gustaf
-- 
Gustaf Rydevik, M.Sci.
tel: +46(0)703 051 451
address:Essingetorget 40,112 66 Stockholm, SE
skype:gustaf_rydevik



More information about the R-help mailing list