读取列名称为日期格式 [英] Read column names as date format

查看:124
本文介绍了读取列名称为日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Excel中的日期如下,第一个col1作为字符,从col2col5的日期格式(mm/dd/yyyy)

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

我尝试使用readxl库将上述文件导入到R中,结果显示日期格式的列名显示为数据集中的数字

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,

如何导入具有相同格式的Excel日期列?

How to import Excel date column with same format?

推荐答案

由于数据集为excel格式,我们可以使用read_excel进行读取,然后将列名称更改为其原始格式

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

这篇关于读取列名称为日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆