shiny
shiny

Reputation: 3502

Change in date format after exporting data.frame from R as .csv

It is a simple question but I couldn't find an answer.

I have this data.frame in R

df <- data.frame(date = as.Date(c("2001-01-05", "2000-05-13", "2000-07-22", "2000-07-22", "1990-11-03")))

I want to export it as .csv file where Month will be abbreviated (%b) and year will be year with century (%Y) as below

df1 <- df %>% mutate(date = format(as.Date(date, "%Y-%m-%d"), "%b-%Y"))
write.csv(df1, "df1.csv")
> df1
      date
1 Jan-2001
2 May-2000
3 Jul-2000
4 Jul-2000
5 Nov-1990

After I opened the df1.csv file, I found a different format for the date as below

enter image description here

I couldn't find the format month(abbreviated)-YEAR. Any suggestion how to fix it?

Upvotes: 2

Views: 2752

Answers (2)

R. Schifini
R. Schifini

Reputation: 9313

You have to change the format in all the date cells in Excel. Go to format cells and then change the format from

mmm-yy

to

mmm-yyyy

This format is not in the the list, change it in the box between "Type" and the format options list

Upvotes: 2

neilfws
neilfws

Reputation: 33772

This is not an R issue. Excel has "helpfully" applied its own formatting to your data. To prevent this, you need to exert more control over the CSV import. These instructions may help you with that.

Upvotes: 3

Related Questions