I have a large list of data frames that all contain the same column headings that I would like to apply the same function with multiple arguments to.
Example list of 2 data frames :
df<- data.frame(Epoch = c(99,126,136,148,150,200,228,247,268,269,285,285,294,308,309,319,320,324,326,337,338,352,353,380,382,418,419,424,431,437,440,447,449,470,515,548,550,550,561,589,590,596,598,603,612,616,623,626,633,655,663,668,682,687,704,708,717,730,744,752,771,794,810,811,816,819,825,832,832,841,847,851,853,868,871,881,893,915,918,922,935,943),
Event = c( "hypopnea","auto_trig","double_trig","auto_trig","double_trig" , "double_trig" ,"hypopnea" ,"hypopnea","auto_trig" ,"double_trig","auto_trig","double_trig" , "double_trig" ,"auto_trig", "double_trig","auto_trig", "double_trig","auto_trig", "double_trig" , "double_trig", "hypopnea","hypopnea" ,"double_trig","double_trig","hypopnea", "double_trig" ,"hypopnea" , "double_trig", "high_leak", "auto_trig", "double_trig", "auto_trig" ,"double_trig", "double_trig", "double_trig", "hypopnea", "auto_trig", "double_trig", "hypopnea", "double_trig", "auto_trig", "double_trig", "auto_trig","double_trig","double_trig","auto_trig", "auto_trig","hypopnea", "double_trig", "double_trig", "double_trig", "auto_trig", "double_trig","auto_trig", "hypopnea" , "double_trig", "double_trig", "double_trig", "double_trig", "ineffective_eff", "ineffective_eff", "double_trig", "double_trig", "high_leak", "double_trig", "hypopnea", "auto_trig", "hypopnea", "double_trig", "high_leak", "double_trig", "high_leak", "auto_trig", "double_trig", "double_trig", "double_trig", "double_trig", "auto_trig", "double_trig", "double_trig", "double_trig", "double_trig"))
df2<- data.frame(Epoch = c(98,99,126,135,141,181,183,200,236,247,257,284,308,353,353,354,380,382,418,418,431,561,733,751,841,851),
Event = c("double_trig", "hypopnea", "auto_trig", "hypopnea", "hypopnea", "double_trig", "hypopnea", "double_trig", "hypopnea", "hypopnea", "hypopnea", "double_trig", "hypopnea", "double_trig", "hypopnea", "double_trig", "hypopnea", "high_leak", "hypopnea", "hypopnea", "ineffective_eff", "high_leak", "high_leak", "double_trig", "double_trig", "ineffective_eff"))
List1<- list(df, df2)
The components of the function that I would like to apply to the list of data frames (with an example on an individual data frame):
df3<- data.frame(Section = seq(1, max(df$Epoch/10+1), by = 1),
Epoch5 = seq(1, max(df$Epoch), by =10),
Epoch51 =seq(11, max(df$Epoch)+10, by = 10))
sp <- split(df$Epoch, df$Event)
nms <- names(sp)
E <- c(df3$Epoch5, max(df3$Epoch51))
df3 <- df3 |>
cbind(
sapply(nms, \(i) {
sec <- findInterval(sp[[i]], E)
df3$Section %in% sec |> as.integer()
})
)
rm(sp, nms, E)
I've tried using combinations of ".," or "~" as placeholders for the "df" with no success (usually error message saying "Epoch" or "Event" does not exist") when I've tried to translate the above code into a function which is then applied to the list of data frames using purrr::map(). Would someone be able to show me what I'm missing?
It looks like you're wanting something like this: