Re: [R] flatten lists
Hi, I hope this helps. Tested to some depth. x1 <- list(name="Jeroen", age=27, married=FALSE, home=list(country=list(name="Netherlands", short="NL"), city="Utrecht")) x2 <- list(name="Jeroen", age=27, married=FALSE, home=list(country=list(name=list(Country1="Netherlands",Country2="Spain"), short=list("NL","SP")), city="Utrecht")) x3 <- list(name="Jeroen", age=27, married=FALSE, home=list(country=list(name=list(Countrygroup= list("Netherlands","Germany"),Country2="Spain"), short=list("NL","SP")), city="Utrecht")) #recursive function x4<-lapply(do.call("c",c(x3,list(recursive=TRUE))),FUN=unlist) x4[2]<-as.numeric(x4[2]) x4[3]<-as.logical(x4[3]) x4 $name [1] "Jeroen" $age [1] 27 $married [1] FALSE $home.country.name.Countrygroup1 [1] "Netherlands" $home.country.name.Countrygroup2 [1] "Germany" $home.country.name.Country2 [1] "Spain" $home.country.short1 [1] "NL" $home.country.short2 [1] "SP" $home.city [1] "Utrecht" > identical(x4,flatlist(x3)) [1] TRUE A.K. - Original Message - From: Jeroen Ooms To: arun Cc: R help Sent: Tuesday, June 26, 2012 6:55 PM Subject: Re: [R] flatten lists Alright, but I need something recursive for lists with arbitrary deepness. On Tue, Jun 26, 2012 at 3:37 PM, arun wrote: > Hi, > > Try: > > do.call("c",do.call("c",x)) > > x1<-do.call("c",do.call("c",x)) > x2<-flatlist(x) > identical(x1,x2) > [1] TRUE > > > > A.K. > > > > - Original Message - > From: Jeroen Ooms > To: Neal Fultz > Cc: r-help@r-project.org > Sent: Tuesday, June 26, 2012 6:23 PM > Subject: Re: [R] flatten lists > > Hmm that doesn't seem to work if the original list is nested more than > 2 levels deep. I should have probably given a better example: > > x <- list(name="Jeroen", age=27, married=FALSE, > home=list(country=list(name="Netherlands", short="NL"), city="Utrecht")) > > > > > On Tue, Jun 26, 2012 at 3:04 PM, Neal Fultz wrote: >> do.call("c", x) >> >> maybe? >> >> On Tue, Jun 26, 2012 at 02:25:40PM -0700, Jeroen Ooms wrote: >>> I am looking for a function to flatten a list to a list of only 1 >>> level deep. Very similar to unlist, however I don't want to turn it >>> into a vector because then everything will be casted to character >>> vectors: >>> >>> x <- list(name="Jeroen", age=27, married=FALSE, >>> home=list(country="Netherlands", city="Utrecht")) >>> unlist(x) >>> >>> This function sort of does it: >>> >>> flatlist <- function(mylist){ >>> lapply(rapply(mylist, enquote, how="unlist"), eval) >>> } >>> >>> flatlist(x) >>> >>> However it is a bit slow. Is there a more native way? >>> >>> __ >>> 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. > > __ > 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. > __ 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.
Re: [R] flatten lists
Hi, Try: do.call("c",do.call("c",x)) x1<-do.call("c",do.call("c",x)) x2<-flatlist(x) identical(x1,x2) [1] TRUE A.K. - Original Message - From: Jeroen Ooms To: Neal Fultz Cc: r-help@r-project.org Sent: Tuesday, June 26, 2012 6:23 PM Subject: Re: [R] flatten lists Hmm that doesn't seem to work if the original list is nested more than 2 levels deep. I should have probably given a better example: x <- list(name="Jeroen", age=27, married=FALSE, home=list(country=list(name="Netherlands", short="NL"), city="Utrecht")) On Tue, Jun 26, 2012 at 3:04 PM, Neal Fultz wrote: > do.call("c", x) > > maybe? > > On Tue, Jun 26, 2012 at 02:25:40PM -0700, Jeroen Ooms wrote: >> I am looking for a function to flatten a list to a list of only 1 >> level deep. Very similar to unlist, however I don't want to turn it >> into a vector because then everything will be casted to character >> vectors: >> >> x <- list(name="Jeroen", age=27, married=FALSE, >> home=list(country="Netherlands", city="Utrecht")) >> unlist(x) >> >> This function sort of does it: >> >> flatlist <- function(mylist){ >> lapply(rapply(mylist, enquote, how="unlist"), eval) >> } >> >> flatlist(x) >> >> However it is a bit slow. Is there a more native way? >> >> __ >> 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. __ 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. __ 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.
Re: [R] flatten lists
Frankly, I'm not sure what you mean, but presumably unlist(yourlist, recurs=FALSE) is not it, right? -- Bert On Tue, Jun 26, 2012 at 2:25 PM, Jeroen Ooms wrote: > I am looking for a function to flatten a list to a list of only 1 > level deep. Very similar to unlist, however I don't want to turn it > into a vector because then everything will be casted to character > vectors: > > x <- list(name="Jeroen", age=27, married=FALSE, > home=list(country="Netherlands", city="Utrecht")) > unlist(x) > > This function sort of does it: > > flatlist <- function(mylist){ > lapply(rapply(mylist, enquote, how="unlist"), eval) > } > > flatlist(x) > > However it is a bit slow. Is there a more native way? > > __ > 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. > -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]] __ 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.
Re: [R] flatten lists
Alright, but I need something recursive for lists with arbitrary deepness. On Tue, Jun 26, 2012 at 3:37 PM, arun wrote: > Hi, > > Try: > > do.call("c",do.call("c",x)) > > x1<-do.call("c",do.call("c",x)) > x2<-flatlist(x) > identical(x1,x2) > [1] TRUE > > > > A.K. > > > > - Original Message - > From: Jeroen Ooms > To: Neal Fultz > Cc: r-help@r-project.org > Sent: Tuesday, June 26, 2012 6:23 PM > Subject: Re: [R] flatten lists > > Hmm that doesn't seem to work if the original list is nested more than > 2 levels deep. I should have probably given a better example: > > x <- list(name="Jeroen", age=27, married=FALSE, > home=list(country=list(name="Netherlands", short="NL"), city="Utrecht")) > > > > > On Tue, Jun 26, 2012 at 3:04 PM, Neal Fultz wrote: >> do.call("c", x) >> >> maybe? >> >> On Tue, Jun 26, 2012 at 02:25:40PM -0700, Jeroen Ooms wrote: >>> I am looking for a function to flatten a list to a list of only 1 >>> level deep. Very similar to unlist, however I don't want to turn it >>> into a vector because then everything will be casted to character >>> vectors: >>> >>> x <- list(name="Jeroen", age=27, married=FALSE, >>> home=list(country="Netherlands", city="Utrecht")) >>> unlist(x) >>> >>> This function sort of does it: >>> >>> flatlist <- function(mylist){ >>> lapply(rapply(mylist, enquote, how="unlist"), eval) >>> } >>> >>> flatlist(x) >>> >>> However it is a bit slow. Is there a more native way? >>> >>> __ >>> 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. > > __ > 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. > __ 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.
Re: [R] flatten lists
Hmm that doesn't seem to work if the original list is nested more than 2 levels deep. I should have probably given a better example: x <- list(name="Jeroen", age=27, married=FALSE, home=list(country=list(name="Netherlands", short="NL"), city="Utrecht")) On Tue, Jun 26, 2012 at 3:04 PM, Neal Fultz wrote: > do.call("c", x) > > maybe? > > On Tue, Jun 26, 2012 at 02:25:40PM -0700, Jeroen Ooms wrote: >> I am looking for a function to flatten a list to a list of only 1 >> level deep. Very similar to unlist, however I don't want to turn it >> into a vector because then everything will be casted to character >> vectors: >> >> x <- list(name="Jeroen", age=27, married=FALSE, >> home=list(country="Netherlands", city="Utrecht")) >> unlist(x) >> >> This function sort of does it: >> >> flatlist <- function(mylist){ >> lapply(rapply(mylist, enquote, how="unlist"), eval) >> } >> >> flatlist(x) >> >> However it is a bit slow. Is there a more native way? >> >> __ >> 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. __ 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.
Re: [R] flatten lists
do.call("c", x) maybe? On Tue, Jun 26, 2012 at 02:25:40PM -0700, Jeroen Ooms wrote: > I am looking for a function to flatten a list to a list of only 1 > level deep. Very similar to unlist, however I don't want to turn it > into a vector because then everything will be casted to character > vectors: > > x <- list(name="Jeroen", age=27, married=FALSE, > home=list(country="Netherlands", city="Utrecht")) > unlist(x) > > This function sort of does it: > > flatlist <- function(mylist){ > lapply(rapply(mylist, enquote, how="unlist"), eval) > } > > flatlist(x) > > However it is a bit slow. Is there a more native way? > > __ > 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. __ 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.
[R] flatten lists
I am looking for a function to flatten a list to a list of only 1 level deep. Very similar to unlist, however I don't want to turn it into a vector because then everything will be casted to character vectors: x <- list(name="Jeroen", age=27, married=FALSE, home=list(country="Netherlands", city="Utrecht")) unlist(x) This function sort of does it: flatlist <- function(mylist){ lapply(rapply(mylist, enquote, how="unlist"), eval) } flatlist(x) However it is a bit slow. Is there a more native way? __ 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.