简体   繁体   中英

Import data in R from Access

I'm trying to import a table from Microsoft Access (.accdb) to R.

The code that I use is:

library(RODBC)

testdb <- file.path("modelEAU Database V.2.accdb")

channel <- odbcConnectAccess2007(testdb)

WQ_data <- sqlFetch(channel, "WaterQuality")

It seems that it works but the problem is importing date and time data. Into the Access file there are two columns, one with date field (dd/mm/yyyy) and another one with time field (hh:mm:ss) and when I import them in R, in date column appears the date with yyyy-mm-dd format and into the time column the format is 1899-12-30 hh:mm:ss. Also, R can't recognise these formats as a variable and I can't work with them.

Also, I tried the mdb.get function but it didn't work as well.

Does somebody know how to import the data in R from Access defining the date and time format ? Any idea how to import the Access file as a text file?

Note: I'm working with with Office 2010 and R version 2.14.1

Thanks a lot in advanced.

Look at the result of runing str on your data frame. That will tell you more about how the data is actually stored. Generally dates and times are stored as a number from an origin date (Access uses 30 Dec. 1899 because MS thought that 1900 was a leap year). Sometimes it is stored as the number of days since the origin with time being represented as a fraction of the day, other times it is the number of seconds (or miliseconds) since the origin.

You will need to see how the data was sent (whether access and odbc converted to strings first, or sent days or seconds), then you will have a better feel for how to work with these (possibly converting) in R.

There is an article in the June 2004 edition of R News (the predecesor to the R Journal) that details the common ways to deal with dates and times in R and could be very useful to you.

You should decide what you want to end up with, a single column of DateTimes, 2 columns with numbers, 2 columns with characters, etc.

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