Massimo Bressan
2013-Oct-03 14:41 UTC
[R] storing element number of a list in a column data frame
#let's suppose I have a list like this mytest<-list(45, NULL, 18, NULL, 99) #to note that this is just an amended example because in fact #I'm dealing with a long list (more than 400 elements) #with no evident pattern of the NULL values #I want to end up with a data frame like the following data.frame(i=c(1,3,5), n=c(45,18,99)) #i.e. a data frame storing in #column i the number of corresponding element list #column n the unique component of that element #I've been trying with do.call(rbind, mytest) #or do.call(rbind.data.frame, mytest) #but this approach is not properly achieving the desired result #now I'm in trouble on how to store each element number of the list in the first column data frame #any help for this? #thanks [[alternative HTML version deleted]]
Hi, You may try: names(mytest)<- 1:length(mytest) mat1<-? do.call(rbind,mytest) dat1<-data.frame(i=row.names(mat1),n=mat1[,1]) ?row.names(dat1)<- 1:nrow(dat1) ?dat1 #? i? n #1 1 45 #2 3 18 #3 5 99 A.K. ----- Original Message ----- From: Massimo Bressan <mbressan at arpa.veneto.it> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Thursday, October 3, 2013 10:41 AM Subject: [R] storing element number of a list in a column data frame #let's suppose I have a list like this mytest<-list(45, NULL, 18, NULL, 99) #to note that this is just an amended example because in fact #I'm dealing with a long list (more than 400 elements) #with no evident pattern of the NULL values #I want to end up with a data frame like the following data.frame(i=c(1,3,5), n=c(45,18,99)) #i.e. a data frame storing in #column i the number of corresponding element list #column n the unique component of that element #I've been trying with do.call(rbind, mytest) #or do.call(rbind.data.frame, mytest) #but this approach is not properly achieving the desired result #now I'm in trouble on how to store each element number of the list in the first column data frame #any help for this? #thanks ??? [[alternative HTML version deleted]] ______________________________________________ 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.
Bert Gunter
2013-Oct-03 14:55 UTC
[R] storing element number of a list in a column data frame
Have you read An Introduction to R (ships with R) or another of the many excellent R tutorials on the web? I ask, because you do not appear to be using a sensible data structure. As your list appears to be of a single type (probably numeric, maybe integer), it would be preferable to use a vector, like this: y <- c(45, NA, 18, NA, 99) (The NULLS must be converted to NA's to "hold" their places). There would then seem to be little need for the data frame structure, as it tends to slow things down in R. But if you insist, which(is.na(y)) will give you the indices of the NA's. See also: ?is.na ?is.null. Cheers, Bert On Thu, Oct 3, 2013 at 7:41 AM, Massimo Bressan <mbressan at arpa.veneto.it> wrote:> #let's suppose I have a list like this > > mytest<-list(45, NULL, 18, NULL, 99) > > #to note that this is just an amended example because in fact > > #I'm dealing with a long list (more than 400 elements) > > #with no evident pattern of the NULL values > > #I want to end up with a data frame like the following > > data.frame(i=c(1,3,5), n=c(45,18,99)) > > #i.e. a data frame storing in > > #column i the number of corresponding element list > > #column n the unique component of that element > > #I've been trying with > > do.call(rbind, mytest) > > #or > > do.call(rbind.data.frame, mytest) > > #but this approach is not properly achieving the desired result > > #now I'm in trouble on how to store each element number of the list in > the first column data frame > > #any help for this? > > #thanks > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Bert Gunter Genentech Nonclinical Biostatistics (650) 467-7374
Sarah Goslee
2013-Oct-03 14:58 UTC
[R] storing element number of a list in a column data frame
Hi, Something like this, maybe:> mytest.df <- data.frame(i=seq_along(mytest)[!sapply(mytest, is.null)], n=unlist(mytest)) > mytest.dfi n 1 1 45 2 3 18 3 5 99 It took me a couple of tries, because I wasn't expecting unlist() to drop the NULL values:> unlist(mytest)[1] 45 18 99 Sarah On Thu, Oct 3, 2013 at 10:41 AM, Massimo Bressan <mbressan at arpa.veneto.it> wrote:> #let's suppose I have a list like this > > mytest<-list(45, NULL, 18, NULL, 99) > > #to note that this is just an amended example because in fact > > #I'm dealing with a long list (more than 400 elements) > > #with no evident pattern of the NULL values > > #I want to end up with a data frame like the following > > data.frame(i=c(1,3,5), n=c(45,18,99)) > > #i.e. a data frame storing in > > #column i the number of corresponding element list > > #column n the unique component of that element > > #I've been trying with > > do.call(rbind, mytest) > > #or > > do.call(rbind.data.frame, mytest) > > #but this approach is not properly achieving the desired result > > #now I'm in trouble on how to store each element number of the list in > the first column data frame > > #any help for this? > > #thanks > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Sarah Goslee http://www.functionaldiversity.org
David Carlson
2013-Oct-03 15:12 UTC
[R] storing element number of a list in a column data frame
Try this> i=which(!sapply(mytest, is.null)) > n=do.call(rbind, mytest[i]) > mydf <- data.frame(i, n) > mydfi n 1 1 45 2 3 18 3 5 99 ------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Massimo Bressan Sent: Thursday, October 3, 2013 9:42 AM To: r-help at r-project.org Subject: [R] storing element number of a list in a column data frame #let's suppose I have a list like this mytest<-list(45, NULL, 18, NULL, 99) #to note that this is just an amended example because in fact #I'm dealing with a long list (more than 400 elements) #with no evident pattern of the NULL values #I want to end up with a data frame like the following data.frame(i=c(1,3,5), n=c(45,18,99)) #i.e. a data frame storing in #column i the number of corresponding element list #column n the unique component of that element #I've been trying with do.call(rbind, mytest) #or do.call(rbind.data.frame, mytest) #but this approach is not properly achieving the desired result #now I'm in trouble on how to store each element number of the list in the first column data frame #any help for this? #thanks [[alternative HTML version deleted]] ______________________________________________ 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.