I have to admit I'm totally stumped on this one, so apologies for no clear attempt, though hopefully I can ask the question clearly:
I have a list of dataframes. For all of them, there are multiple date-type variables that need to be formatted as a date (e.g., as.Date(data$var, format = "%m/%d/%y")).
The problem is that the date variable is named something different in each of the dataframes. In the example below, we've got "start_date" and "end_date".
Is there a way to write some function that operates over the variable names in a dataframe and if it finds text that contains "date", will do the formatting operation?
The dataframes:
west <- data.frame(
spend = sample(50:100,50,replace=T),
trials = sample(100:200,50,replace=T),
start_date = sample(c("06/07/14","06/08/14","06/09/14"), 50, replace=T),
country = sample(c("usa","canada","uk"),50,replace = T)
)
east <- data.frame(
end_date = sample(c("06/07/14","06/08/14","06/09/14"), 50, replace=T),
spend = sample(50:100,50,replace=T),
trials = sample(100:200,50,replace=T),
country = sample(c("china","japan","skorea"),50,replace = T)
)
And turning them into a list (in reality, this is a much larger list):
combined <- c(west,east)
How could I take the logical vector from a grepl statement and tell it to operate on the variable where that logical vector is "TRUE" across the list elements?
grepl("date", names(combined))
[1] FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
list(west, east)and applygreplon that list. - akrunc(west, east), which is not a list of data frames), but applyinggreplon that list won't work. - David Robinson