write out list of lists with names
try this:
x <- 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"))
for (i in names(x)){
+ cat(i, "\n")
+ print(x[[i]])
+ }
MU10
MU.16 MU.19 MU.21 mean sd
0.8052791 0.4350489 0.4551954 0.5651744 0.2081806
MU11
MU.21 MU.22 mean sd
0.56061566 0.65200918 0.60631242 0.06462498
MU12
MU.14 MU.20 MU.23 mean sd
0.7726512 0.3925776 0.3822244 0.5158177 0.2224845
MU13
MU.20 MU.22 mean sd
0.3636058 0.2139613 0.2887835 0.1058146
MU14
MU.18 mean sd
0.3169202 0.3169202 NA
MU15
MU.18 MU.22 mean sd
0.5764542 0.8236923 0.7000733 0.1748237
On Wed, Nov 28, 2012 at 10:30 AM, Iain Gallagher
<iaingallagher at btopenworld.com> wrote:
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
______________________________________________ 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.