I have a list of multiple dataframes imported from CSV files with patient information at two different timepoints. I’ve been trying to convert a time variable from character to datetime via POSIXct using lapply. I would like to reference the time variable via a value called ‘timepoint’ and I noticed when I use mutate(across(contains(timepoint), ~as.POSIXct()), nothing happens. I don’t get an error message, I just notice that my time variable didn’t convert.
Here is some example data and my code:
<code>test1 <- as.data.frame(structure(list(EncounterID=structure(c(1, 2, 3, 4, 5, 6)),
xtimestamp=structure(c("03/08/2018 12:00",
"03/17/2018 05:32",
"02/11/2018 09:15",
"04/01/2019 15:10",
"04/30/2018 06:05",
"04/18/2018 18:45")))))
test2 <- as.data.frame(structure(list(EncounterID=structure(c(1, 2, 3, 4, 5, 6)),
xtimestamp=structure(c("09/08/2018 12:00",
"09/17/2018 05:32",
"09/11/2018 09:15",
"09/01/2019 15:10",
"09/30/2018 06:05",
"09/18/2018 18:45")))))
list1<-list(test1, test2)
timepoint<-"xtimestamp"
list2<-lapply(list1, function(df){
df%>%
mutate(across(contains(timepoint), ~as.POSIXct(.x , format="%m/%d/%Y %H:%M")))
})
</code>
<code>test1 <- as.data.frame(structure(list(EncounterID=structure(c(1, 2, 3, 4, 5, 6)),
xtimestamp=structure(c("03/08/2018 12:00",
"03/17/2018 05:32",
"02/11/2018 09:15",
"04/01/2019 15:10",
"04/30/2018 06:05",
"04/18/2018 18:45")))))
test2 <- as.data.frame(structure(list(EncounterID=structure(c(1, 2, 3, 4, 5, 6)),
xtimestamp=structure(c("09/08/2018 12:00",
"09/17/2018 05:32",
"09/11/2018 09:15",
"09/01/2019 15:10",
"09/30/2018 06:05",
"09/18/2018 18:45")))))
list1<-list(test1, test2)
timepoint<-"xtimestamp"
list2<-lapply(list1, function(df){
df%>%
mutate(across(contains(timepoint), ~as.POSIXct(.x , format="%m/%d/%Y %H:%M")))
})
</code>
test1 <- as.data.frame(structure(list(EncounterID=structure(c(1, 2, 3, 4, 5, 6)),
xtimestamp=structure(c("03/08/2018 12:00",
"03/17/2018 05:32",
"02/11/2018 09:15",
"04/01/2019 15:10",
"04/30/2018 06:05",
"04/18/2018 18:45")))))
test2 <- as.data.frame(structure(list(EncounterID=structure(c(1, 2, 3, 4, 5, 6)),
xtimestamp=structure(c("09/08/2018 12:00",
"09/17/2018 05:32",
"09/11/2018 09:15",
"09/01/2019 15:10",
"09/30/2018 06:05",
"09/18/2018 18:45")))))
list1<-list(test1, test2)
timepoint<-"xtimestamp"
list2<-lapply(list1, function(df){
df%>%
mutate(across(contains(timepoint), ~as.POSIXct(.x , format="%m/%d/%Y %H:%M")))
})
Thank you!