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)

  • 相关阅读:
    Laravel 初始化
    ant design pro 左上角 logo 修改
    请求到服务端后是怎么处理的
    Websocket 知识点
    王道数据结构 (7) KMP 算法
    王道数据结构 (6) 简单的模式匹配算法
    王道数据结构 (4) 单链表 删除节点
    王道数据结构 (3) 单链表 插入节点
    王道数据结构 (2) 单链表 尾插法
    王道数据结构 (1) 单链表 头插法
  • 原文地址:https://www.cnblogs.com/JaniceZD/p/7978425.html
Copyright © 2011-2022 走看看