简体   繁体   中英

reading xlsx file using read_xlsx in R with first row as month-year variable

I have a xlsx data file where the first row is time var given in format (ie jan-53 feb-53). I used this command to read the data but R is converting the non-year as number. Following is my command. wpi_raw<- read_excel("/Users/sonu/Dropbox/vd_ps/wpi_monthly.xlsx",sheet=1, col_names = TRUE,col_types=NULL,na="",skip=0) Please suggest how do I go about reading the data with read_excel. I am providing the dropbox link of the data for reference.
<a href="https://www.dropbox.com/s/vpm8hbk63zc1jcd/wpi_monthly.xlsx?dl=0">dpi_monthly</a>

When you import dates from excel, you have to convert them from the days since epoch to what you want...

library("readxl")
tmp <- tempfile(fileext=".xlsx")
download.file("https://www.dropbox.com/s/vpm8hbk63zc1jcd/wpi_monthly.xlsx?dl=1",
              destfile=tmp, mode="wb")
wpi_raw <- read_excel(tmp, sheet=1, col_names = TRUE,col_types=NULL,na="",skip=0)

names(wpi_raw)[2:ncol(wpi_raw)] <- format(as.Date(as.numeric(names(wpi_raw)[2:ncol(wpi_raw)]),
                                           origin="1900-01-01"), "%b-%y")
> names(wpi_raw)
 [1] "commodity" "Apr-53"    "May-53"    "Jun-53"    "Jul-53"   
 [6] "Aug-53"    "Sep-53"    "Oct-53"    "Nov-53"    "Dec-53"   
[11] "Jan-54"    "Feb-54"    "Mar-54"  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM