Help with this much appreciated I have a large dataframe that I would like to subset where the constraint Test1 <- subset(df, date == uniques[[1]]), where uniques is a list of dates that must be matched to create Test1. I would like to perform an operation on Test1 that results in a single column of data. So far so good. How do loop through all values in the uniques list (say there is 50), perform an operationon Test1,,,,Test50, and then bolt all the lists together in a single list please ? Regards Glenn [[alternative HTML version deleted]]
you can try lapply(lapply(uniques, function(x) subset(df, date == x)), myfun) or possibly more accurate (subset may be finicky due to scoping): lapply(lapply(uniques, function(x) df[df$date == x, ]), myfun) or use ?split lapply(split(df, df$date), myfun) HTH, --sundar On Sun, Feb 8, 2009 at 5:00 PM, glenn <g1enn.roberts at btinternet.com> wrote:> Help with this much appreciated > > > > I have a large dataframe that I would like to subset where the constraint > > > > Test1 <- subset(df, date == uniques[[1]]), where uniques is a list of dates > that must be matched to create Test1. > > > > I would like to perform an operation on Test1 that results in a single > column of data. So far so good. > > > > How do loop through all values in the uniques list (say there is 50), > perform an operationon Test1,,,,Test50, and then bolt all the lists together > in a single list please ? > > > > Regards > > > > > > Glenn > > > > > [[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. >
See if this illustration using the %in% operator within subset() is helpful: > df1 <- data.frame(x=1:10, y=sample(c("a","b","c"), 10, replace=TRUE) ) > uniques <- list("a","b") > > Test1 <- subset(df1, y %in% uniques) > Test1 x y 1 1 b 4 4 a 5 5 b 6 6 b 7 7 a 9 9 a Next question of course is whether you were using the word "list" in an r-specific fashion? Fortunately, I think %in% will also work with vector input. You might not want to make 50 Test<n>'s. That would be very much against the spirit of R. Provide a simpler example involving 3 or 4 lists and someone might step up and solve it. Of course, I may have given you a one step solution if you were thinking that uniques[[1]] was a single number. Might be best to name your dataframe something other than df which is also valid function name for the density of the F distribution. -- David Winsemius On Feb 8, 2009, at 8:00 PM, glenn wrote:> Help with this much appreciated > > > > I have a large dataframe that I would like to subset where the > constraint > > > > Test1 <- subset(df, date == uniques[[1]]), where uniques is a list > of dates > that must be matched to create Test1. > > > > I would like to perform an operation on Test1 that results in a single > column of data. So far so good. > > > > How do loop through all values in the uniques list (say there is 50), > perform an operationon Test1,,,,Test50, and then bolt all the lists > together > in a single list please ? > > > > Regards > > > > > > Glenn > > > > > [[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.