zoukankan      html  css  js  c++  java
  • R语言学习(10)获取数据

    1.Web数据

      a. 拥有API的网站

    > library(WDI)
    载入需要的程辑包:RJSONIO
    > library(RJSONIO)
    > library(WDI)
    > wdi_datasets <- WDIsearch()
    > head(wdi_datasets)
    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)"
    > wdi_trade_in_services <-WDI(indicator = "BG.GSR.NFSV.GD.ZS")
    > str(wdi_trade_in_services)
    'data.frame': 1848 obs. of 4 variables:
    $ iso2c : chr "1A" "1A" "1A" "1A" ...
    $ country : chr "Arab World" "Arab World" "Arab World" "Arab World" ...
    $ BG.GSR.NFSV.GD.ZS: num 15 17.1 19.1 17.4 18.5 ...
    $ year : num 2011 2010 2009 2008 2007 ...

      使用quantmod包可以访问股票行情

    > library(quantmod)

    > options(getSymbols.auto.assign=FALSE)
    > microsoft <- getSymbols("MSFT")

    > head(microsoft)
    MSFT.Open MSFT.High MSFT.Low
    2007-01-03 29.91 30.25 29.40
    2007-01-04 29.70 29.97 29.44
    2007-01-05 29.63 29.75 29.45
    2007-01-08 29.65 30.10 29.53
    2007-01-09 30.00 30.18 29.73
    2007-01-10 29.80 29.89 29.43
    MSFT.Close MSFT.Volume MSFT.Adjusted
    2007-01-03 29.86 76935100 22.96563
    2007-01-04 29.81 45774500 22.92717
    2007-01-05 29.64 44607200 22.79642
    2007-01-08 29.93 50220200 23.01947
    2007-01-09 29.96 44636600 23.04254
    2007-01-10 29.66 55017400 22.81180

       b.抓取网页

    抓取Justin Rao的网站从2002年到2008年间的NBA工资数据

    > salary_url <- "http://www.justinmrao.com/salary_data.csv"
    > local_copy <- "my local copy.csv"
    > download.file(salary_url,local_copy)
    trying URL 'http://www.justinmrao.com/salary_data.csv'
    Content type 'text/plain' length 390643 bytes (381 KB)
    downloaded 381 KB

    > salary_data <- read.csv(local_copy)

  • 相关阅读:
    UDP通信网络编程
    多线程:购票小案例
    多线程:线程池异步计算,2个线程,1个计算10的阶乘,一个计算20的阶乘并返回
    Kong入门指南
    5分钟完成App后端API开发
    Docker安装Kong
    React Native如何用调用RestFul API
    Flutter如何用调用RestFul API
    如何用Flutter调用生成的RestFul API
    自动化API之一 生成开源ERP Odoo App 的RestFul API
  • 原文地址:https://www.cnblogs.com/JaniceZD/p/7978425.html
Copyright © 2011-2022 走看看