0
votes

Trying to change multiple CHR type columns to date using mutate_at() in .

UPDATE:

Here is the tibble. Note I was able to use a regular mutate with mdy_hm() on a single variable but not multiple in mutate_at()

head(data)
# A tibble: 6 x 52
  CONTACTID Status `Contract Date` `Country of Ori~ `CES Submitted` `Embassy Date` `Hire Date` Agent `ATT Received` `CES Issued Dat~ `CES Ready Revi~ `CES RFR 8 week~ `I-140 Sent` `I-140 Recd.` `I-140 Result D~ `I-140 Status` `CES Status` `Choice of Agen~ Client `Contact Owner`
  <chr>     <chr>  <chr>           <chr>            <chr>           <chr>          <chr>       <chr> <chr>          <chr>            <chr>            <lgl>            <chr>        <chr>         <chr>            <chr>          <chr>        <chr>            <chr>  <chr>          
1 zcrm_257~ Finis~ NA              Philippines      NA              NA             6/22/2015   NA    NA             NA               NA               FALSE            NA           NA            NA               NA             NA           NA               Oakbe~ Tyler Richards 
2 zcrm_257~ Finis~ 12/15/2015 0:00 England          NA              12/5/2016      2/20/2017   Adev~ NA             NA               NA               FALSE            4/14/2016    6/29/2011     4/25/2016        Approved       NA           5/25/2016        Oakbe~ Eddie Money    
3 zcrm_257~ Finis~ 11/9/2015 6:00  Philippines      NA              NA             11/17/2015  Ulti~ NA             NA               NA               FALSE            NA           NA            NA               NA             NA           NA               Oakbe~ Eddie Money    
4 zcrm_257~ Finis~ 3/3/2016 21:00  Philippines      NA              NA             3/21/2016   NA    NA             NA               NA               FALSE            NA           NA            NA               NA             NA           NA               Oakbe~ Eddie Money    
5 zcrm_257~ Finis~ 8/15/2006 0:00  Philippines      NA              3/21/2016      6/27/2016   IQ    NA             NA               NA               FALSE            3/1/2007     3/2/2007      3/8/2007         Approved       NA           7/10/2007        Oakbe~ alyssa Coleman 
6 zcrm_257~ Relea~ 7/20/2016 6:00  Philippines      NA              NA             9/12/2016   Ulti~ NA             NA               NA               FALSE            NA           NA            NA               NA             NA           NA               Oakbe~ Eddie Money    
# ... with 32 more variables: `DS260 Completed` <chr>, Elite <lgl>, `Embassy Status` <chr>, `Fee Bill Received` <chr>, `Fee Bill Returned` <chr>, `Hospital Country` <chr>, `Hospital Size` <dbl>, `IELTS Test Date` <chr>, `Initial Ready for Review` <chr>, `Instruction Pkt.
#   Returned` <chr>, `Lead Source` <chr>, `Name of Hospital` <chr>, `NCLEX Pass/Fail` <chr>, `NCLEX Ready 90 days` <lgl>, `NCLEX Review Purchased` <chr>, `NCLEX Review Name` <chr>, `NCLEX Test Date` <chr>, `NVC Completed` <chr>, `Mailing City` <chr>, `Mailing Country` <chr>,
#   `Mailing State` <chr>, `Previous Rcpt Date` <lgl>, `RFE Issue Date` <chr>, Specialty <chr>, `State Board Approved Date` <chr>, `Submittal Date` <chr>, `UTM Source` <chr>, `UTM Medium` <chr>, `UTM Content` <chr>, `Visa Screen Approved` <chr>, `Years as RN` <dbl>,
#   `Termination Date` <chr>

Just using two here as example:

data <- data %>% mutate_at(data,vars(`Contract Date`,`Hire Date`),
                             funs(as_date(mdy_hm())))

Which yields the following error:

Error: `.vars` must be a character/numeric vector or a `vars()` object, not a `tbl_df/tbl/data.frame` object Run `rlang::last_error()` to see where the error occurred.

Any help as to how I can avoid this error?

1
Using the pipe means you don't need to add data2 again in mutate calls or other callsNelsonGon

1 Answers

3
votes

Contract Date and Hire Date have different formats. Try :

library(dplyr)
library(lubridate)

data2 %>% 
  mutate(`Contract Date` = as_date(mdy_hm(`Contract Date`)), 
         `Hire Date` = mdy(`Hire Date`))

We can also use base R to do this :

transform(df, `Contract Date` = as.Date(as.POSIXct(`Contract Date`, 
                                format = "%m/%d/%Y %H:%M")), 
              `Hire Date` = as.Date(`Hire Date`, "%m/%d/%Y"))