Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read column names as date format

Tags:

datetime

r

readxl

I have date in Excel as follows, first col1 as char and from col2 to col5 is in date format (mm/dd/yyyy)

id   1/1/2016   2/1/2016  3/1/2016  4/1/2016  5/1/2016
1     23         545       33         55          66
2     454        536       66         80          11
3     83         585        9         10          19

I tried to import the above file in to R using readxl library, and my result shows column names which are in date format shows as number in dataset,

How to import Excel date column with same format?

like image 290
user5860640 Avatar asked Mar 13 '17 06:03

user5860640


1 Answers

As the dataset is in excel format, we can read it with read_excel and then change the column names to its original format

library(readxl)
library(dplyr)
read_excel("yourdata.xlsx" %>% 
    setNames(., c('id', format(as.Date(as.numeric(names(.)[-1]), 
                   origin = '1899-12-30'), '%m/%d/%Y')))
like image 194
akrun Avatar answered Oct 19 '22 17:10

akrun