An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110517/39479ab9/attachment.pl>
subsetting a list of dataframes
8 messages · Lara Poplarski, Jorge Ivan Velez, William Dunlap +3 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110517/f2c3e1f3/attachment.pl>
shouldKeep <- sapply(listOfDataFrames, function(df)nrow(df)>1) listOfDataFrames[shouldKeep] or, compressed to get rid of the intermediate variable listOfDataFrames[sapply(listOfDataFrames, function(df)nrow(df)>1)] If you are writing production code and there is any chance that listOfDataFrames might be an empty list you can use vapply (which requires that you supply a prototype for FUN's return value): listOfDataFrames[vapply(listOfDataFrames, function(df)nrow(df)>1, FUN.VALUE=FALSE)] Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Lara Poplarski Sent: Tuesday, May 17, 2011 1:25 PM To: r-help at r-project.org Subject: [R] subsetting a list of dataframes Hello All, I have a list of dataframes, and I need to subset it by keeping only those dataframes in the list that meet a certain criterion. Specifically, I need to generate a second list which only includes those dataframes whose number of rows is > 1. Could someone suggest how to do this? I have come close to what I need with loops and such, but there must be a less clumsy way... Many thanks, Lara [[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.
On 18/05/11 08:24, Lara Poplarski wrote:
Hello All, I have a list of dataframes, and I need to subset it by keeping only those dataframes in the list that meet a certain criterion. Specifically, I need to generate a second list which only includes those dataframes whose number of rows is> 1. Could someone suggest how to do this? I have come close to what I need with loops and such, but there must be a less clumsy way...
L.new <- L[tapply(L,nrow) > 1]
cheers,
Rolf Turner
Have a look at lapply(). Something like: entries.with.nrows=lapply(data,function(x)dim(x)[1]>1) should give you a vector with the elements of the list that you seek marked with TRUE. This vector can then be used to extract a subset from your list by: data.reduced=data[entries.with.nrows] Or similar.... HTH Jannis --- Lara Poplarski <larapoplarski at gmail.com> schrieb am Di, 17.5.2011:
Von: Lara Poplarski <larapoplarski at gmail.com> Betreff: [R] subsetting a list of dataframes An: r-help at r-project.org Datum: Dienstag, 17. Mai, 2011 20:24 Uhr Hello All, I have a list of dataframes, and I need to subset it by keeping only those dataframes in the list that meet a certain criterion. Specifically, I need to generate a second list which only includes those dataframes whose number of rows is > 1. Could someone suggest how to do this? I have come close to what I need with loops and such, but there must be a less clumsy way... Many thanks, Lara ??? [[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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110517/c80d70c4/attachment.pl>
On May 17, 2011, at 7:13 PM, Lara Poplarski wrote:
Thank you all, this is exactly what I had in mind, except that I still have to get my head around apply et al. Back to the books for me then!
Read the lapply( ...) call as: "For every element in the object named `data`, send that element to a function that returns TRUE if its first dimension is greater than one, returns FALSE if its first dimension is one, and return nothing (actually a vector with zero elements) if it doesn't have a (first) "dim" attribute, and finally return the ordered collection of those values as a list which is assigned the name 'entries.with.nrows'. "
Lara On Tue, May 17, 2011 at 2:41 PM, Jannis <bt_jannis at yahoo.de> wrote:
Have a look at lapply(). Something like: entries.with.nrows=lapply(data,function(x)dim(x)[1]>1) should give you a vector with the elements of the list that you seek marked with TRUE. This vector can then be used to extract a subset from your list by: data.reduced=data[entries.with.nrows] Or similar.... HTH Jannis --- Lara Poplarski <larapoplarski at gmail.com> schrieb am Di, 17.5.2011:
Von: Lara Poplarski <larapoplarski at gmail.com> Betreff: [R] subsetting a list of dataframes An: r-help at r-project.org Datum: Dienstag, 17. Mai, 2011 20:24 Uhr Hello All, I have a list of dataframes, and I need to subset it by keeping only those dataframes in the list that meet a certain criterion. Specifically, I need to generate a second list which only includes those dataframes whose number of rows is > 1. Could someone suggest how to do this? I have come close to what I need with loops and such, but there must be a less clumsy way... Many thanks, Lara [[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.
[[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.
David Winsemius, MD Heritage Laboratories West Hartford, CT
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Lara Poplarski Sent: Tuesday, May 17, 2011 4:14 PM To: r-help at r-project.org Subject: Re: [R] subsetting a list of dataframes Thank you all, this is exactly what I had in mind, except that I still have to get my head around apply et al. Back to the books for me then! Lara On Tue, May 17, 2011 at 2:41 PM, Jannis <bt_jannis at yahoo.de> wrote:
Have a look at lapply(). Something like: entries.with.nrows=lapply(data,function(x)dim(x)[1]>1)
Note that the above suggestion does not work in R 2.13.0:
> listOfDataFrames <- list(three=data.frame(x=11:13,y=101:103),
one=data.frame(x=1,y=2),
five=data.frame(x=1:5,y=11:15))
> listOfDataFrames[lapply(listOfDataFrames,function(x)nrow(x)>1)]
Error in listOfDataFrames[lapply(listOfDataFrames, function(x) nrow(x)
:
invalid subscript type 'list' lapply(...) always returns a list and lists are not acceptable as subscripts. Instead, make the subscript one of the following: as.logical(lapply(...)) sapply(...) # and hope that FUN always returns TRUE or FALSE and length(list)>0 vapply(..., FUN.VALUE=FALSE) It may be a bit quicker to do the >0 outside of the loop, as in as.integer(lapply(listOfDataFrames, FUN=nrow)) > 0 or vapply(listOfDataFrames, FUN=nrow, FUN.VALUE=0L) > 0 but you need a pretty long list to notice. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
should give you a vector with the elements of the list that
you seek marked
with TRUE. This vector can then be used to extract a subset from your list by: data.reduced=data[entries.with.nrows] Or similar.... HTH Jannis --- Lara Poplarski <larapoplarski at gmail.com> schrieb am Di,
17.5.2011:
Von: Lara Poplarski <larapoplarski at gmail.com>
Betreff: [R] subsetting a list of dataframes
An: r-help at r-project.org
Datum: Dienstag, 17. Mai, 2011 20:24 Uhr
Hello All,
I have a list of dataframes, and I need to subset it by
keeping only those
dataframes in the list that meet a certain criterion.
Specifically, I need
to generate a second list which only includes those
dataframes whose number
of rows is > 1.
Could someone suggest how to do this? I have come close to
what I need with
loops and such, but there must be a less clumsy way...
Many thanks,
Lara
[[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.
[[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.