r - 如何从 URL 读取表格并将其保存为数据框?

标签 r url dataframe read.table

我对函数 read.table 有疑问。我想从 url 中读取一个表,并将其作为数据框保存在 R 中。网址是: https://datanalytics.com/uploads/datos_treemap.txt

我写了这段代码:

library(RCurl)

a <- getURL('https://datanalytics.com/uploads/datos_treemap.txt')
b = read.table(a, sep="\t ", header = TRUE, nrows=3)

download.file("https://datanalytics.com/uploads/datos_treemap.txt","/mnt/M/Ana/R/datos_treemap.txt",method = c("wget"))

但我无法将数据保留为数据帧,这会导致以下错误:

Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") : cannot open file...
No such file or directory

我也试过把文档下载成txt格式,保存在小米PC里。但是 generate txt 生成一个向量而不是一个表(所有结果都在一个 unic 行中)。我写的代码是:

download.file("https://datanalytics.com/uploads/datos_treemap.txt","/mnt/M/Ana/R/datos_treemap.txt",method = c("wget"))

我做错了什么?

最佳答案

library(RCurl)
a <- getURL('https://datanalytics.com/uploads/datos_treemap.txt')
b <- read.table(text=a, header = TRUE)

关于r - 如何从 URL 读取表格并将其保存为数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47217562/

相关文章:

r - R中具有整数变量的非线性优化/编程

r - 使用 group_by 和 case_when 为每组生成单个值

带有 at 符号的 URL 被截断

url - 可以在 URL 中包含 ID 吗?

android - 在Android应用程序中单击按钮时如何打开网站?

python - 使用 Pandas str.split 和不同长度的逗号分隔字符串

r - 提取单个dplyr tbl_df行作为矢量

arrays - 在向量前面添加零

python-3.x - 将上市公司数据的 HTML 读取到 Dataframe 中

r - 超过100个类别的散布堆积条形图