Apricot
Apricot

Reputation: 3011

Subsetting list of data frames using lapply and ifelse

I have list of data frames:

d1<-data.frame(a=rnorm(5), b=c(rep(2006, times=4),NA))
d2<-data.frame(a=1:5, b=c(2007, 2007, NA, NA, 2007))  
d <- list(d1,d2)

I want to subset the dataframes based on the value of column b, in the first row.

d <- lapply(d, function(x) ifelse(x[1,2] == 2006, x[-1,], x))

I tried with the above code. Instead of returning the list of data frames with the first row in data frame 1 removed, I got the following:

[[1]]
[[1]][[1]]
[1] -1.0604320 -1.1117245 -0.2147006  1.0389965


[[2]]
[[2]][[1]]
[1] 1 2 3 4 5

The expected output is as follows:

[[1]]
           a    b
2 -1.0604320 2006
3 -1.1117245 2006
4 -0.2147006 2006
5  1.0389965   NA

[[2]]
  a    b
1 1 2007
2 2 2007
3 3   NA
4 4   NA
5 5 2007

Upvotes: 0

Views: 62

Answers (2)

Roman
Roman

Reputation: 17648

A tidyverse

library(tidyverse)
d %>% 
  map(~filter(.,1:n() != 1 | b != 2006))
[[1]]
            a    b
1  2.14889697 2006
2  0.05842311 2006
3  1.89911545 2006
4 -0.16836139   NA

[[2]]
  a    b
1 1 2007
2 2 2007
3 3   NA
4 4   NA
5 5 2007

Upvotes: 0

Andre Elrico
Andre Elrico

Reputation: 11480

d1<-data.frame(a=rnorm(5), b=c(rep(2006, times=4),NA))
d2<-data.frame(a=1:5, b=c(2007, 2007, NA, NA, 2007))  
d <- list(d1,d2)

d <- lapply(d, function(x) if(x[1,2] == 2006) x[-1,] else x)

#[[1]]
#           a    b
#2  0.5125665 2006
#3 -0.9842669 2006
#4 -1.9572385 2006
#5 -0.9411411   NA

#[[2]]
#  a    b
#1 1 2007
#2 2 2007
#3 3   NA
#4 4   NA
#5 5 2007

Upvotes: 2

Related Questions