gpt4 book ai didi

r - 如何在R中加载示例数据集?

转载 作者:行者123 更新时间:2023-12-04 03:02:52 24 4
gpt4 key购买 nike

假设我要重现发布在StackOverflow上的示例。有些人建议张贴者使用 dput() to help streamline this processdatasets available in the base package之一。

但是,在这种情况下,假设只给了我数据帧的输出:

> site.data
site year peak
1 ALBEN 5 101529.6
2 ALBEN 10 117483.4
3 ALBEN 20 132960.9
8 ALDER 5 6561.3
9 ALDER 10 7897.1
10 ALDER 20 9208.1
15 AMERI 5 43656.5
16 AMERI 10 51475.3
17 AMERI 20 58854.4

除了将其另存为文本文件并使用 read.table()之外,我还有其他选择吗?

最佳答案

那是一个很好的解决方案。我猜想有一种方法可以使用RCurl as in this post which scraped off wikipedia做到这一点。

但是作为一个更一般的讨论点:为什么我们不只使用R中“数据集”包中的数据呢?然后,每个人只需调用data()函数就可以获取数据,并且有涵盖大多数情况的数据集。

[编辑]:我能够做到这一点。显然,这比您的解决方案还要工作(即不切实际)。 :)

[编辑2]:我将其包装到一个函数中,并在另一页上进行了尝试。

getSOTable <- function(url, code.block=2, raw=FALSE, delimiter="code") {
require(RCurl)
require(XML)

webpage <- getURL(url)
webpage <- readLines(tc <- textConnection(webpage)); close(tc)
pagetree <- htmlTreeParse(webpage, error=function(...){}, useInternalNodes = TRUE)
x <- xpathSApply(pagetree, paste("//*/", delimiter, sep=""), xmlValue)[code.block]
if(raw)
return(strsplit(x, "\n")[[1]])
else
return(read.table(textConnection(strsplit(x, "\n")[[1]][-1])))
}

getSOTable("https://stackoverflow.com/questions/1434897/how-do-i-load-example-datasets-in-r")
site year peak
1 ALBEN 5 101529.6
2 ALBEN 10 117483.4
3 ALBEN 20 132960.9
8 ALDER 5 6561.3
9 ALDER 10 7897.1
10 ALDER 20 9208.1
15 AMERI 5 43656.5
16 AMERI 10 51475.3
17 AMERI 20 58854.4

getSOTable("https://stackoverflow.com/questions/1428174/quickly-generate-the-cartesian-product-of-a-matrix", code.block=10)
X1 X2 X3 X4
1 1 11 1 11
2 1 11 2 12
3 1 11 3 13
4 1 11 4 14
5 1 11 5 15
6 1 11 6 16
7 1 11 7 17
8 1 11 8 18
9 1 11 9 19
10 1 11 10 20

关于r - 如何在R中加载示例数据集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1434897/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com