R使用getURL数据到数据框

R using getURL data to dataframe

我正在从网上下载数据,但不知道如何将其更改为数据框或任何有用的东西。有没有人有什么建议?这是代码:

library(RCurl) 
myfile = getURL(http://www.stat.ufl.edu/~winner/data/lister_ul.dat,
ssl.verifyhost=FALSE, ssl.verifypeer=FALSE)

如果我使用这个:

A = read.csv(textConnection(myfile), header = F)

然后 R 理解为:

c("1 1 1")

作为第一行而不是这个:

c(1, 1, 1).

这行不通 b/c 我需要使用

colnames(A) = c("col1", "col2", "col3")

并且无法找到不涉及使用

进行一些繁琐工作的解决方法
unlist(strsplit(A))

呃!!

如有任何建议,我们将不胜感激。或者,如有必要,我可能会编写自己的繁琐函数。

格温

这有帮助吗?

df <- read.table('http://www.stat.ufl.edu/~winner/data/lister_ul.dat')

仅使用基础包函数:

as.data.frame(
    do.call("rbind", strsplit(
        readLines("http://www.stat.ufl.edu/~winner/data/lister_ul.dat"),
        "\s+"))
)

  V1 V2 V3
1  1  1  1
2  1  0 11
3  0  1  6
4  0  0  6

我们所做的是从网页上读取原始行,然后用返回的字符之间的空格拆分每一行,然后通过在每一行上调用 rbind 创建一个矩阵...然后我们翻译它放入数据框中。

你很接近。因为我没有安装 RCurl 但我有 httr(它使用 curl),我将从它开始。不过,这是一个没有实际意义的问题,因为我获得了与您相同的 table-looking 内容。

此外,@udden2903 的回答更 straight-forward,我假设这是一个简化的问题,您可能需要继续使用 read.table(URL) 的替代获取方法不允许。 (要继续使用 httr 并支持其他一些功能,例如身份验证,请阅读其文档。)

library(httr)
myfile = GET("http://www.stat.ufl.edu/~winner/data/lister_ul.dat")
str(content(myfile))
# No encoding supplied: defaulting to UTF-8.
#  chr "1 1  1\n1 0 11\n0 1  6\n0 0  6\n"

因此,content(myfile) 现在就是您的 myfile。第一个技巧是你的数据不是comma-delimited("csv"),所以使用read.table是必要的。其次,您需要指定第一行不是 headers.

x <- read.table(textConnection(content(myfile, encoding = "UTF-8")), header = FALSE)
x
#   V1 V2 V3
# 1  1  1  1
# 2  1  0 11
# 3  0  1  6
# 4  0  0  6

现在只需分配您的 headers。

colnames(x) <- c("col1", "col2", "col3")
x
#   col1 col2 col3
# 1    1    1    1
# 2    1    0   11
# 3    0    1    6
# 4    0    0    6