0

This should be easy but I'm losing my mind here.

I used read.csv to read an excel .csv file. I can see that when I run class on the column it is currently read as a character type. I need R to instead read it as a date type.

I've tried using as.Date, as.Date.character, and tried lubridate's functions, and nothing has worked. Each time I try I get an error that says: "charToDate(x): character string is not in a standard unambiguous format" Or the object I try populating the value to comes back blank. I've read several websites and questions posted on stackoverflow, and none of them work. I think it is because most questions get answered when the string is not directly from a .csv file (I can get it to work if I manually type it into R Studio, but that defeats the point of my question).

I tried going back into excel and manually changing the cells to a different date format but because it is .csv, it forces it back into the "3/18/2023" format.

I'm sure others have tried converting a date to character from a .csv file. How?

Phil
  • 7,287
  • 3
  • 36
  • 66
Steven
  • 150
  • 1
  • 2
  • 14

1 Answers1

-1

In case anyone else comes across this problem, I still don't know how to do it with .csv; however, if you can save your excel file as .xls or .xlsx, then I found a solution:

install.packages(tidyverse)
library(readxl)

df <- read_excel("YourFile.xlsx")

test <- as.Date(df$DateColumn)

I'm not sure why R Studio likes the same date column when imported using this method as opposed to the .csv method, but it doesn't throw back that error anymore. Hopefully someone can post a solution that doesn't involve re-saving the excel file as a different format, but this worked for what I needed so it answered my question.

Steven
  • 150
  • 1
  • 2
  • 14
  • Given the fact that there are many duplicates of this question, it seems pointless to offer an answer to a somewhat similar but not duplicated question. Rather you should do a search for a duplicate and suggest it in a comment. – IRTFM Mar 19 '23 at 04:10
  • Thanks IRTFM, but I did search for an answer for over an hour (brave and google) and my search results never resulted in finding an answer I needed. It then took me another hour of searching before I finally found something that works. Glad Phil found an answer that seems like something that could work. Haven't tried it since I am happy with my solution, but I wish that question was easier to find. Hopefully my question will help others who search with similar terms. – Steven Mar 25 '23 at 12:25