迷途mitu 发表于 2013-6-12 23:11
公司有买,那怎么用RODBC呢?本人在学R但是不太清楚连接数据库的方法。。。求教。。
From A Comma Delimited Text File
- # first row contains variable names, comma is separator
- # assign the variable id to row names
- # note the / instead of \ on mswindows systems
- mydata <- read.table("c:/mydata.csv", header=TRUE,
- sep=",", row.names="id")
复制代码From Excel
The best way to read an Excel file is to export it to a comma delimited file and import it using the method above. On windows systems you can use the RODBC package to access Excel files. The first row should contain variable/column names.
- # first row contains variable names
- # we will read in workSheet mysheet
- library(RODBC)
- channel <- odbcConnectExcel("c:/myexel.xls")
- mydata <- sqlFetch(channel, "mysheet")
- odbcClose(channel)
复制代码- # From Access
- library(RODBC)
- channel <- odbcConnectAccess("c:/myacc.mdb")
- mydata <- sqlFetch(channel, "mytable")
- odbcClose(channel)
复制代码From SPSS
- # save SPSS dataset in trasport format
- get file='c:\mydata.sav'.
- export outfile='c:\mydata.por'.
- # in R
- library(Hmisc)
- mydata <- spss.get("c:/mydata.por", use.value.labels=TRUE)
- # last option converts value labels to R factors
复制代码From SAS
- # save SAS dataset in trasport format
- libname out xport 'c:/mydata.xpt';
- data out.mydata;
- set sasuser.mydata;
- run;
- # in R
- library(Hmisc)
- mydata <- sasxport.get("c:/mydata.xpt")
- # character variables are converted to R factors
复制代码From Stata
- # input Stata file
- library(foreign)
- mydata <- read.dta("c:/mydata.dta")
复制代码From systat
- # input Systat file
- library(foreign)
- mydata <- read.systat("c:/mydata.dta")
复制代码