Re: [R] Convert list of lists <--> data frame

From: N. Lapidus <n.lapidus_at_gmail.com>
Date: Wed, 23 Jul 2008 16:52:54 +0200

Not very elegant but seems to work:

pats.df <- as.data.frame(t(sapply (1:length(pats), function (i) do.call(cbind,pats[[i]]))))
colnames(pats.df) <- names(pats[[1]])
# then
pats2 <- lapply (1:nrow(pats.df), function (i) as.list(t(pats.df)[,i]))

Nael

On Wed, Jul 23, 2008 at 3:23 PM, Michael Friendly <friendly_at_yorku.ca> wrote:
>
> For a function that takes an argument as a list of lists of parameters, I'd like to be able to convert that
> to a data.frame and vice versa, but can't quite figure out how.
>
> pats <- list(structure(list(shape = 0, shape.col = "black", shape.lty = 1,
> cell.fill = "white", back.fill = "white", label = 1, label.size = 1,
> ref.col = "gray80", ref.grid = "yes", scale.max = 100), .Names = c("shape",
> "shape.col", "shape.lty", "cell.fill", "back.fill", "label",
> "label.size", "ref.col", "ref.grid", "scale.max")), structure(list(
> shape = 0, shape.col = "black", shape.lty = 1, cell.fill = "pink",
> back.fill = "white", label = 1, label.size = 1, ref.col = "gray80",
> ref.grid = "yes", scale.max = 100), .Names = c("shape", "shape.col",
> "shape.lty", "cell.fill", "back.fill", "label", "label.size",
> "ref.col", "ref.grid", "scale.max")), structure(list(shape = 0,
> shape.col = "black", shape.lty = 1, cell.fill = "red", back.fill = "white",
> label = 1, label.size = 1, ref.col = "gray80", ref.grid = "yes",
> scale.max = 100), .Names = c("shape", "shape.col", "shape.lty",
> "cell.fill", "back.fill", "label", "label.size", "ref.col", "ref.grid",
> "scale.max")))
>
> So, I want pats.df to have 10 columns,
> c("shape", "shape.col", "shape.lty", "cell.fill", "back.fill", "label",
> "label.size", "ref.col", "ref.grid", "scale.max"), and 3 rows for this example.
>
> Given pats.df, I'd want to turn that back to pats.
>
> thanks for any help,
> -Michael
>
> --
> Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept.
> York University Voice: 416 736-5115 x66249 Fax: 416 736-5814
> 4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html
> Toronto, ONT M3J 1P3 CANADA
>
> ______________________________________________
> 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.



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 Wed 23 Jul 2008 - 15:58:07 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 Wed 23 Jul 2008 - 16:32:10 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