Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

r - Read column names as date format

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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')))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...