Skip to content

subsets problem

3 messages · glenn, Sundar Dorai-Raj, David Winsemius

#
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:
#
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.