Convert multiple columns to dates with lubridate and dplyr

≯℡__Kan透↙ 提交于 2019-12-20 02:28:13

问题


I'm looking for a straightforward way to convert all of the variables in a data frame which begin with 'date' to dates using lubridate::dmy() (they are currently characters with the dmy format).

I had thought there would be done with mutate_if or mutate_each in dplyr but I am struggling to figure out how.


回答1:


You can use mutate_at()

library(dplyr)
library(lubridate)

df <- mutate_at(df, vars(starts_with("date")), funs(dmy))

or use mutate_if to mutate all date columns to dmy. Using is.Date from lubridate.

df  <- mutate_if(df, is.Date, funs(dmy))


来源:https://stackoverflow.com/questions/43264068/convert-multiple-columns-to-dates-with-lubridate-and-dplyr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!