3

I have a list of dataframes. I would like to filter each df base on the value of certain variables that are included in dtvarlst. Is it possible to use map() method to loop through the whole list and get what I want? Could anyone guide me on this? Or if you know any other way to do it.

The data and my attempt code:

df1<- structure(list(ID = c(1, 2, 3, 4, 5), STDT = structure(c(1751328000, 
1751414400, 1751500800, 1751587200, 1751673600), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Grade = c(98, 56, 32, 88, 75), ENDT = structure(c(1751760000, 
1751846400, 1751932800, 1752019200, 1752105600), class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -5L))

df2 <-structure(list(ID = c(2, 5, 9, 7, 6), STDT1 = structure(c(1750809600, 
1750896000, 1750982400, 1751068800, 1751155200), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), RFDT2 = structure(c(1751328000, 1751414400, 
1751500800, 1751587200, 1751673600), class = c("POSIXct", "POSIXt"
), tzone = "UTC")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-5L))

lst1 <- list(df1, df2)
dtvar <- c("STDT", "ENDT")

lst2<- map(lst1,          
           function(.x) 
           .x <-.x %>% 
            filter(any_of(one_of(dtvar))<=ymd("2025-07-03")) # Did not work#
           ) 

1 Answer 1

2

Up front, two things: ymd() and any_of().

Realize that ymd() is returning a Date, and numerical dates are "day == 1" whereas numerical posixt is second == 1, so ... clearly not going to be a correct comparison.

You can work around this by wrapping ymd() in as.POSIXct or by including tz=:

class(ymd("2025-07-03"))
# [1] "Date"

ymd("2025-07-03", tz="UTC")
# [1] "2025-07-03 UTC"
class(ymd("2025-07-03", tz="UTC"))
# [1] "POSIXct" "POSIXt" 

as.POSIXct(ymd("2025-07-03"))
# [1] "2025-07-03 UTC"
class(as.POSIXct(ymd("2025-07-03")))
# [1] "POSIXct" "POSIXt" 

Additionally, any_of is for selecting columns, not really intended within filter(.) for this. I think you want if_any().

library(purrr)
library(lubridate)
map(lst1, \(x) filter(x, if_any(any_of(dtvar),
                                ~ .x <= as.POSIXct(ymd("2025-07-03")))))
# [[1]]
# # A tibble: 3 × 4
#      ID STDT                    Grade ENDT                   
#   <dbl> <dttm>                  <dbl> <dttm>                 
# 1     1 2025-07-01 00:00:00.000    98 2025-07-06 00:00:00.000
# 2     2 2025-07-02 00:00:00.000    56 2025-07-07 00:00:00.000
# 3     3 2025-07-03 00:00:00.000    32 2025-07-08 00:00:00.000
# [[2]]
# # A tibble: 5 × 3
#      ID STDT1                   RFDT2                  
#   <dbl> <dttm>                  <dttm>                 
# 1     2 2025-06-25 00:00:00.000 2025-07-01 00:00:00.000
# 2     5 2025-06-26 00:00:00.000 2025-07-02 00:00:00.000
# 3     9 2025-06-27 00:00:00.000 2025-07-03 00:00:00.000
# 4     7 2025-06-28 00:00:00.000 2025-07-04 00:00:00.000
# 5     6 2025-06-29 00:00:00.000 2025-07-05 00:00:00.000
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.