Access a URL and read Data with R

后端 未结 5 1313
醉酒成梦
醉酒成梦 2020-11-27 02:54

Is there a way I can specify and get data from a web site URL on to a CSV file for analysis using R?

5条回答
  •  旧时难觅i
    2020-11-27 03:39

    base

    read.csv without the url function just works fine. Probably I am missing something if Dirk Eddelbuettel included it in his answer:

    ad <- read.csv("http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv")
    head(ad)
    

      X    TV radio newspaper sales
    1 1 230.1  37.8      69.2  22.1
    2 2  44.5  39.3      45.1  10.4
    3 3  17.2  45.9      69.3   9.3
    4 4 151.5  41.3      58.5  18.5
    5 5 180.8  10.8      58.4  12.9
    6 6   8.7  48.9      75.0   7.2
    

    Another options using two popular packages:

    data.table

    library(data.table)
    ad <- fread("http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv")
    head(ad)
    

    V1    TV radio newspaper sales
    1:  1 230.1  37.8      69.2  22.1
    2:  2  44.5  39.3      45.1  10.4
    3:  3  17.2  45.9      69.3   9.3
    4:  4 151.5  41.3      58.5  18.5
    5:  5 180.8  10.8      58.4  12.9
    6:  6   8.7  48.9      75.0   7.2
    

    readr

    library(readr)
    ad <- read_csv("http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv")
    head(ad)
    

    # A tibble: 6 x 5
         X1    TV radio newspaper sales
              
    1     1 230.1  37.8      69.2  22.1
    2     2  44.5  39.3      45.1  10.4
    3     3  17.2  45.9      69.3   9.3
    4     4 151.5  41.3      58.5  18.5
    5     5 180.8  10.8      58.4  12.9
    6     6   8.7  48.9      75.0   7.2
    

提交回复
热议问题