[R] How to export tables in list separately using write.table or sink?

From: Lauri Nikkinen <lauri.nikkinen_at_iki.fi>
Date: Fri, 29 Feb 2008 08:21:59 +0200


R users,

My intention is to take factors out of DF, create list of tables and export these tables separately using write.table or sink function. write.table writes tables out as DF:s, should I use sink instead?

Here is my example:

a <- data.frame( indx = 1:20,

                    var1 =    rep(c("I20", "I40", "A50", "B60"), each=5),
                    var1_lab= rep(c("cat", "dog", "mouse", "horse"), each=5),
                    var2 =    rep(c("B20", "X40", "D50", "G60"), each=5),
                    var2_lab= rep(c("car", "bicycle", "train", "bus"), each=5))

g <- unlist(lapply(a, is.factor))
g <- a[g]
g.tablist <- lapply(g, function(x) table(x, g$var1))

for (i in g.tablist) write.table(i , paste(Sys.Date(), i, sep=""))

Warning messages:
1: In if (file == "") file <- stdout() else if (is.character(file)) { :   the condition has length > 1 and only the first element will be used 2: In file(file, ifelse(append, "a", "w")) :

or using lapply

lapply(g.tablist, function(i) write.table(i , paste(Sys.Date(), i, sep="")))
$var1

NULL
$var1_lab

NULL
$var2

NULL
$var2_lab

NULL Warning messages:
1: In if (file == "") file <- stdout() else if (is.character(file)) { :   the condition has length > 1 and only the first element will be used 2: In file(file, ifelse(append, "a", "w")) :

#or
for (i in g.tablist) { sink(paste(i, ".txt.", sep=""))

                       sink()}

Warning messages:
1: In file(file, ifelse(append, "a", "w")) :   only first element of 'description' argument used 2: In file(file, ifelse(append, "a", "w")) :   only first element of 'description' argument used 3: In file(file, ifelse(append, "a", "w")) :   only first element of 'description' argument used 4: In file(file, ifelse(append, "a", "w")) :   only first element of 'description' argument used

Thanks,
Lauri



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. Received on Fri 29 Feb 2008 - 06:26:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 29 Feb 2008 - 12:30:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive