I've got a large data set, with numeric, character and a few date variables.
structure(list(DOB = structure(c(18155, 18164,
18785, 18328, 18314, 18307, 18324), class = "Date"), date_today_ppt_SEEQ = structure(c(18155,
18164, 18785, 18328, 18314, 18307, 18324), class = "Date"), switching_home = c("Sometimes",
"Most of the time", "Sometimes", "Sometimes", "Rarely", "Sometimes",
"Rarely"), single_lang_environm_home = c(80, 0, 100, 75, 95,
70, 30), dual_lang_environm_home = c(20, 60, 0, 23, 0, 20, 70
), dense_code_sw_home = c(0, 40, 0, 2, 5, 10, 0), between_sentence_sw_home = c("Sometimes",
"Most of the time", "Sometimes", "Sometimes", "Never", "Sometimes",
"Rarely"), within_sentence_sw_home = c("Sometimes", "Most of the time",
"Most of the time", "Rarely", "Rarely", "Rarely", "Sometimes"
)), row.names = c(NA, 7L), class = "data.frame")
I'm trying to just recode character values to numeric across using:
exampledata[exampledata == "Always"] <- 100
exampledata[exampledata == "Frequently"] <- 75
exampledata[exampledata == "Most of the time"] <- 75
exampledata[exampledata == "Sometimes"] <- 50
exampledata[exampledata == "Rarely"] <- 25
exampledata[exampledata == "Never"] <- 0
When I try to do that, I get the error:
Error in charToDate(x) :
character string is not in a standard unambiguous format
I suspect it has to do with the fact I have date format in my dataset (which comes from an xlsx file), so I've done a few things because I read it might be a problem with the locale or the format of the date.
exampledata$DOB <- openxlsx::convertToDate(exampledata$DOB)
exampledata$DOB <- as.Date(exampledata$DOB, format = "%d/%m/%y")#recorde as DD/MM/YYYY
exampledata$DOB <- lubridate::ymd(exampledata$DOB, locale = "English")
Someone suggested using mutate, so I also tried:
exampledata <- mutate(exampledata, DOB = as.Date(DOB, "%d/%m/%y"))
When I run:
> class(exampledata$DOB)
[1] "Date"
It clearly shows up as date. However, when I open my data frame in a window to explore visually and point the cursor to the variable, "column 1: unknown" appears under my cursor, which makes me think it didn't convert to the expected (?) date format.
I read through people's similar problem but am not sure why it shows up as date when I run class and still creates problems. Also, I'm supposedly only addressing values in the character variables, so not sure why it creates a problem at all. Also, people seem to talk about it but nowhere I found what are actually these standard unambiguous values for date?
Finally, as I was creating the reproducible example using dput, I could see my date is converted to number but when I print the column, it prints dates, so I'm really confused:
exampledata$DOB
[1] "2019-09-16" "2019-09-25" "2021-06-07" "2020-03-07" "2020-02-22" "2020-02-15" "2020-03-03"
If anyone has an idea, I'd be glad for some help here.
Finally, here are is my version info (OS is Windows):
> R.version.string
[1] "R version 4.0.3 (2020-10-10)"