读取excel文件用到了xlsx 包,用read.xlsx2来读取
> install.packages('xlsx')
> library(xlsx) 载入需要的程辑包:rJava 载入需要的程辑包:xlsxjars > > bike_data <- system.file("extdata","Alpe d'Huez.xls",package = "learningr") > bike_file <- read.xlsx2(bike_data,sheetIndex = 1,startRow = 2,endRow = 38,colIndex = 2:8,colClasses = c("character","numeric","character","integer","character","character","character")) Error in loadWorkbook(file) : Cannot find > head (bike_data)要加载foreign包
R还可以读取很多其他类型的文件
访问WEB上的数据 加载WDI包
通过网站的API接口直接下载到R系统
世界银行的数据已经直接公布
> library(WDI) 载入需要的程辑包:RJSONIO > wdi <- WDIsearch() > head(wdi) indicator [1,] "BG.GSR.NFSV.GD.ZS" [2,] "BM.KLT.DINV.GD.ZS" [3,] "BN.CAB.XOKA.GD.ZS" [4,] "BN.CUR.GDPM.ZS" [5,] "BN.GSR.FCTY.CD.ZS" [6,] "BN.KLT.DINV.CD.ZS" name [1,] "Trade in services (% of GDP)" [2,] "Foreign direct investment, net outflows (% of GDP)" [3,] "Current account balance (% of GDP)" [4,] "Current account balance excluding net official capital grants (% of GDP)" [5,] "Net income (% of GDP)" [6,] "Foreign direct investment (% of GDP)"
使用download.file先下载好文件,之后再进行读入
更高级的网络访问可以用Rcul包来进行
访问数据库,用到了DBI包
具体代码就不写了,附上几张照片吧