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
Try this: pats.df <- do.call(rbind, pats) On Wed, Jul 23, 2008 at 10:23 AM, 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
on 07/23/2008 08:23 AM Michael Friendly 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, > -MichaelMichael, One approach for the first issue: > do.call(rbind, lapply(pats, data.frame)) shape shape.col shape.lty cell.fill back.fill label label.size 1 0 black 1 white white 1 1 2 0 black 1 pink white 1 1 3 0 black 1 red white 1 1 ref.col ref.grid scale.max 1 gray80 yes 100 2 gray80 yes 100 3 gray80 yes 100 Given that: > lapply(seq(along = rownames(pats.df)), function(i) as.list(pats.df[i, ])) [[1]] [[1]]$shape [1] 0 [[1]]$shape.col [1] black Levels: black [[1]]$shape.lty [1] 1 [[1]]$cell.fill [1] white Levels: white pink red [[1]]$back.fill [1] white Levels: white [[1]]$label [1] 1 [[1]]$label.size [1] 1 [[1]]$ref.col [1] gray80 Levels: gray80 [[1]]$ref.grid [1] yes Levels: yes [[1]]$scale.max [1] 100 [[2]] [[2]]$shape [1] 0 [[2]]$shape.col [1] black Levels: black [[2]]$shape.lty [1] 1 [[2]]$cell.fill [1] pink Levels: white pink red [[2]]$back.fill [1] white Levels: white [[2]]$label [1] 1 [[2]]$label.size [1] 1 [[2]]$ref.col [1] gray80 Levels: gray80 [[2]]$ref.grid [1] yes Levels: yes [[2]]$scale.max [1] 100 [[3]] [[3]]$shape [1] 0 [[3]]$shape.col [1] black Levels: black [[3]]$shape.lty [1] 1 [[3]]$cell.fill [1] red Levels: white pink red [[3]]$back.fill [1] white Levels: white [[3]]$label [1] 1 [[3]]$label.size [1] 1 [[3]]$ref.col [1] gray80 Levels: gray80 [[3]]$ref.grid [1] yes Levels: yes [[3]]$scale.max [1] 100 HTH, Marc Schwartz
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.
try ?unlist it may help regards +------------------------------------------------- | Patrizio Frederic | Research associate in Statistics, | Department of Economics, | University of Modena and Reggio Emilia, | Via Berengario 51, | 41100 Modena, Italy | | tel: +39 059 205 6727 | fax: +39 059 205 6947 | mail: patrizio.frederic at unimore.it +------------------------------------------------- 2008/7/23 Michael Friendly <friendly at yorku.ca>:> 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. >
Michael Friendly <friendly <at> yorku.ca> writes:> > 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. >[snip data] pats.df <- do.call("rbind",pats) pats2 <- apply(pats.df,1,as.list) identical(pats,pats2) Ben Bolker
Dear Michael, Perhaps, data.frame(do.call(rbind,lapply(pats,function(x) t(as.matrix(x,ncol=10))))) HTH, Jorge On Wed, Jul 23, 2008 at 9:23 AM, Michael Friendly <friendly@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@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. >[[alternative HTML version deleted]]