Home > OS >  Looping through dataframe to filter rows
Looping through dataframe to filter rows

Time:03-18

In a given dataframe, I need to filter the rows on separate columns, one at a time, using the same condition. The following formulation does not work. Any suggestions?

DF <- data.frame(A = c(1,4,99), 
                 B = c(2,5,6),
                 C = c(3,99,7))

r <- c("A", "C")

for (i in r){
  column = as.formula(paste0("DF$",i))
  DF<- DF[column != 99,]
  print(DF)
}

The desired outputs are the following two:

  A B  C
1 1 2  3
2 4 5 99

   A B C
1  1 2 3
3 99 6 7

CodePudding user response:

We may use

library(dplyr)
library(purrr)
map(r, ~ DF %>%
      filter(!! rlang::sym(.x) != 99))

-output

[[1]]
  A B  C
1 1 2  3
2 4 5 99

[[2]]
   A B C
1  1 2 3
2 99 6 7

Or in base R

lapply(r, \(x) subset(DF, DF[[x]] != 99))
[[1]]
  A B  C
1 1 2  3
2 4 5 99

[[2]]
   A B C
1  1 2 3
3 99 6 7

If it is to filter and then remove on a loop

library(data.table)
setDT(df)
for(nm in r) {
   tmp <- DF[DF[[nm]] != 99]
   ... do some calc ...
   rm(tmp)
   gc()
}
  • Related