Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
访问URL并使用R读取数据_Url_R - Fatal编程技术网

访问URL并使用R读取数据

访问URL并使用R读取数据,url,r,Url,R,是否有一种方法可以使用R指定并从网站URL获取数据到CSV文件以进行分析?在最简单的情况下,只需这样做即可 X <- read.csv(url("http://some.where.net/data/foo.csv")) 2020年底编辑。原文继续 详细回答:是的,这是可以做到的,许多软件包已经使用该功能多年了。这些软件包正是利用这一功能从雅虎下载股票价格的!近十年来: R> library(tseries) Loading required package:

是否有一种方法可以使用R指定并从网站URL获取数据到CSV文件以进行分析?

在最简单的情况下,只需这样做即可

X <- read.csv(url("http://some.where.net/data/foo.csv"))
2020年底编辑。原文继续

详细回答:是的,这是可以做到的,许多软件包已经使用该功能多年了。这些软件包正是利用这一功能从雅虎下载股票价格的!近十年来:

R> library(tseries)
Loading required package: quadprog
Loading required package: zoo

    ‘tseries’ version: 0.10-24

    ‘tseries’ is a package for time series analysis and computational finance.

    See ‘library(help="tseries")’ for details.

R> get.hist.quote("IBM")
trying URL 'http://chart.yahoo.com/table.csv?    ## manual linebreak here
  s=IBM&a=0&b=02&c=1991&d=5&e=08&f=2011&g=d&q=q&y=0&z=IBM&x=.csv'
Content type 'text/csv' length unknown
opened URL
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
.......... .......... .......... .......... ..........
........
downloaded 258 Kb

             Open   High    Low  Close
1991-01-02 112.87 113.75 112.12 112.12
1991-01-03 112.37 113.87 112.25 112.50
1991-01-04 112.75 113.00 111.87 112.12
1991-01-07 111.37 111.87 110.00 110.25
1991-01-08 110.37 110.37 108.75 109.00
1991-01-09 109.75 110.75 106.75 106.87
[...]

这在
帮助(连接)
帮助(url)
的手册页面中都有非常详细的记录。另请参阅R随附的“数据导入/导出”手册。

scan
可以自动从网页读取数据;你不必搞乱连接。

网页上的数据通常是XML表的形式。您可以使用将XML表读入R

在这个包中,函数

readHTMLTable(<url>)
readHTMLTable()
将在页面中查找XML表并返回数据帧列表(找到的每个表一个)。

read.csv(url(“…”)旁边
您还可以使用
read.table(“http://...)

例如:

> sample <- read.table("http://www.ats.ucla.edu/stat/examples/ara/angell.txt")
> sample
                V1   V2   V3   V4 V5
1        Rochester 19.0 20.6 15.0  E
2         Syracuse 17.0 15.6 20.2  E
...
43         Atlanta  4.2 70.6 32.6  S
> 
>示例
V1 V2 V3 V4 V5
1罗切斯特19.0 20.6 15.0 E
2雪城17.0 15.6 20.2东
...
43亚特兰大4.270.6 32.6 S
> 
base
read.csv
没有
url
功能就可以正常工作。如果Dirk Eddelbuettel在他的回答中包含了这一点,我可能遗漏了什么:

ad <- read.csv("http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv")
head(ad)
使用两个流行软件包的其他选项:

数据表 读取器
库(readr)

ad Meth认为您需要连接才能访问(毕竟是远程)网页。连接是一种奇妙的抽象,它允许您以一致的方式使用文件、URL、从命令等pp到标准输出的管道。我并不怀疑连接的有用性,但扫描的帮助文件说“文件也可以是一个完整的URL。”我在没有正式连接的情况下就这样做了,而且确实有效。但您通常不希望
扫描
,而是希望
读取.table()
读取.csv()
,这样您就可以更高级别地访问。True。我意识到现在我读的是标题而不是问题,上面写的是CSV文件。如果文件的格式是非标准的,您(可能)需要
scan
。(这就是我使用它的情况。)在最简单的情况下,只需执行
X如何关闭此连接?我尝试了此方法,但无效。有人能帮忙吗<代码>cv_今天可能是错误的URL。导航到该文件,在web ui中选择
raw
,使用该URL。这是一个很好的答案,广泛用于从Github中读取
csv
数据。您的答案在我的答案出现多年之后,事实上,代码已更改为支持更直接的方法。但是当我写我的答案时,它是不可用的。
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
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
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
  <int> <dbl> <dbl>     <dbl> <dbl>
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