dplyr中带有日期的Mutate_if或mutate_at

时间:2019-05-06 20:47:29

标签: r dplyr lubridate

我有一个超过100列的数据集,但是例如,假设我有一个看起来像

的数据集
 @Override
    public void showLoading() {
        hideLoading();
        mProgressDialog = CommonUtils.showLoadingDialog(this.getContext());
    }

    @Override
    public void hideLoading() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.cancel();
        }
    }

大多数日期(dput(tib) structure(list(f_1 = c("A", "O", "AC", "AC", "AC", "O", "A", "AC", "O", "O"), f_2 = c("New", "New", "New", "New", "Renewal", "Renewal", "New", "Renewal", "New", "New"), first_dt = c("07-MAY-18", "25-JUL-16", "09-JUN-18", "22-APR-19", "03-MAR-19", "10-OCT-16", "08-APR-19", "27-FEB-17", "02-MAY-16", "26-MAY-15"), second_dt = c(NA, "27-JUN-16", NA, "18-APR-19", "27-FEB-19", "06-OCT-16", "04-APR-19", "27-FEB-17", "25-APR-16", NA), third_dt = c("04-APR-16", "21-JUL-16", "05-JUN-18", "18-APR-19", "27-FEB-19", "06-OCT-16", "04-APR-19", "27-FEB-17", "25-APR-16", "19-MAY-15"), fourth_dt = c("05-FEB-15", "25-JAN-16", "05-JUN-18", "10-OCT-18", "08-JAN-19", "02-SEP-16", "24-OCT-18", "29-SEP-16", "27-JAN-15", "14-MAY-15"), fifth_dt = structure(c(1459728000, 1469059200, 1528156800, 1555545600, 1551225600, 1475712000, 1554336000, 1488153600, 1461542400, 1431993600), class = c("POSIXct", "POSIXt" ), tzone = "UTC"), sex = c("M", "M", "F", "F", "M", "F", "F", "F", "F", "F")), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")) )列都是字符串,但是我想将它们转换为日期。我尝试了ends_with(dt),但收到了以下消息:

mutate_at

对导致此错误的原因有何想法?我应该使用其他tib %>% mutate_at(vars(ends_with("dt")), funs(parse_date_time(.))) %>% glimpse() Error in mutate_impl(.data, dots) : Evaluation error: argument "orders" is missing, with no default. 函数吗?

2 个答案:

答案 0 :(得分:4)

如akrun所述,其中一列已采用dttm格式。一旦忽略该列,以下代码将对我起作用:

tib %>% 
  select(-fifth_dt) %>% 
  mutate_at(vars(ends_with("dt")), parse_date_time, orders = "%d-%m-%y")

答案 1 :(得分:2)

funs已过时。可以使用list

library(dplyr)
tib %>% 
   mutate_at(3:6, list(~ parse_date_time(., "%d-%m-%y")))