[R] How to export tables in list separately using write.table or sink?
Lauri Nikkinen
lauri.nikkinen at iki.fi
Fri Feb 29 07:21:59 CET 2008
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
More information about the R-help
mailing list