[R] write out list of lists with names
Iain Gallagher
iaingallagher at btopenworld.com
Wed Nov 28 16:30:42 CET 2012
Hello List
I have a list question. I'm doing some data wrangling for a colleague and I have nested list in the following format:
structure(list(MU10 = structure(c(0.80527905920989, 0.4350488707836,
0.455195366623, 0.565174432205497, 0.208180556861924), .Names = c("MU.16",
"MU.19", "MU.21", "mean", "sd")), MU11 = structure(c(0.56061565798878,
0.65200918021661, 0.606312419102695, 0.0646249793238221), .Names = c("MU.21",
"MU.22", "mean", "sd")), MU12 = structure(c(0.77265115449472,
0.3925776107826, 0.38222435807226, 0.515817707783193, 0.222484520748552
), .Names = c("MU.14", "MU.20", "MU.23", "mean", "sd")), MU13 = structure(c(0.36360576458114,
0.21396125968483, 0.288783512132985, 0.105814644179484), .Names = c("MU.20",
"MU.22", "mean", "sd")), MU14 = structure(c(0.31692017862428,
0.31692017862428, NA), .Names = c("MU.18", "mean", "sd")), MU15 = structure(c(0.57645424339545,
0.82369227173036, 0.700073257562905, 0.174823686402807), .Names = c("MU.18",
"MU.22", "mean", "sd"))), .Names = c("MU10", "MU11", "MU12",
"MU13", "MU14", "MU15"))
I would like to write this to a text file in the form e.g. (each x is a value):
MU10
MU.16 MU.19 MU.21 mean sd
x x x x x
MU11
MU.21 MU.22 mean sd
x x x x
Where each list element is on a new block of three rows.
After consulting Google I came across the following:
fnlist <- function(x, fil){
z <- deparse(substitute(x))
cat(z, "\n", file = fil)
nams <- names(x)
for (i in seq_along(x) ){ cat(nams[i], "\n", x[[i]], "\n",file = fil, append = TRUE) }
}
fnlist(holdList, 'res.txt')
However this doesn't print the names within each sub list.
Can anyone advise?
Thanks
Iain
R version 2.15.1 (2012-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8
[5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] plyr_1.7.1 gsubfn_0.6-5 proto_0.3-9.2
loaded via a namespace (and not attached):
[1] tcltk_2.15.1 tools_2.15.1
Iain
More information about the R-help
mailing list