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

Henrique Dallazuanna wwwhsd at gmail.com
Fri Feb 29 12:22:56 CET 2008


Try this:

sapply(1:length(g.tablist),
         function(x)write.table(g.tablist[[x]],
file=sprintf('Table%d.txt', x), quote=FALSE))

On 29/02/2008, Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote:
> 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.
>


-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list