0

I do not quite understand how R doesn't seem to like referencing variables inside functions. Can any one help: #first I create this global variable var1. Next I show the df I'm working with:

> var1 <- "read_date"
> str(en_data2)
'data.frame':   4383 obs. of  5 variables:
 $ read_date  : chr  "2006-01-01" "2006-01-02" "2006-01-03" "2006-01-04" ...
 $ Consumption: num  1069 1381 1443 1457 1477 ...

trying to mutate the read_date col from chr to date, this code works when I explicitly reference read_date. but I want to abstract things because I am trying to use mutate inside a function and it won't take the argument when the argument is passed as a string either. But here, even in the global environment it doesn't like it either. it's really confusing, I tried replacing it with %var1 $var1 [, var1]...constipation is the result.

>en_data3 <-
  en_data2 %>% mutate(var1 = as.Date(var1))
Error in `mutate()`:
! Problem while computing `var1 = as.Date(var1)`.
Caused by error in `charToDate()`:
! character string is not in a standard unambiguous format
Run `rlang::last_error()` to see where the error occurred.
Amir Charkhi
  • 603
  • 6
  • 19

0 Answers0