从GitHub Gist加载数据

3

我有一份数据需要在R中导入。

一种方法是从Gist下载数据,然后使用

read.delim 
read.table

但我更希望找到其他方法,而不是下载它,然后使用上面的函数。

下面是一个示例数据:

https://gist.github.com/anonymous/2c69ab500bfa94d0268a


1
library(httr) ; read.table(text=content(GET("https://gist.githubusercontent.com/anonymous/2c69ab500bfa94d0268a/raw/cdaedc27897d4e570f61317711c3f548430570eb/example.txt"), as="text"), header=TRUE) - hrbrmstr
1
df <- read.table("https://gist.githubusercontent.com/anonymous/2c69ab500bfa94d0268a/raw/cdaedc27897d4e570f61317711c3f548430570eb/example.txt", header=TRUE, stringsAsFactors=FALSE, sep="") 对我有效。 - Khashaa
2
@Khasha 这是一个仅适用于 Windows 的解决方案。 - hrbrmstr
2个回答

4
你可以使用类似以下代码的方式:
url <- 'https://gist.githubusercontent.com/anonymous/2c69ab500bfa94d0268a/raw/example.txt'
library(RCurl)
library(bitops)
df <- getURL(url, ssl.verifypeer=FALSE)
df1 <- read.delim(textConnection(df),header=TRUE, row.names=1,stringsAsFactors=FALSE)
dim(df1)

1

我一直在开发一个相关的软件包,叫做rio可以在GitHub上获取。安装后,您可以在一行命令中完成以下操作:

# install and load rio
library("devtools")
install_github("leeper/rio")
library("rio")

# import
> d <- import("https://gist.githubusercontent.com/anonymous/2c69ab500bfa94d0268a/raw/cdaedc27897d4e570f61317711c3f548430570eb/example.txt")

结果:

> str(d)
'data.frame':   1679 obs. of  19 variables:
 $ probes: chr  "200645_at" "200690_at" "200691_s_at" "200692_s_at" ...
 $ M1    : num  0.0446 -0.0165 0.0554 0 0.0608 ...
 $ M2    : num  0.0744 0.1121 -0.0689 -0.0505 0.0601 ...
 $ M3    : num  -0.034 -0.0959 -0.0852 -0.0508 0.0115 ...
 $ M4    : num  0.0173 0 0.0702 -0.0159 0.0744 ...
 $ M5    : num  0.228 -0.4595 0.0823 -0.3041 -0.0232 ...
 $ M6    : num  0.007 -0.0282 0.0361 -0.0684 -0.1095 ...
 $ M7    : num  -0.025 -0.1617 -0.0306 -0.0644 -0.0416 ...
 $ M8    : num  0.0644 -0.0482 -0.0076 -0.0175 -0.0499 ...
 $ M9    : num  -0.0253 -0.2611 -0.034 0.0503 -0.0515 ...
 $ M10   : num  -0.123 0.0223 -0.0198 0.0546 0.0303 ...
 $ M11   : num  -0.625 -0.613 -0.182 -0.214 -0.115 ...
 $ M12   : num  0.021 0.1961 -0.0681 -0.0216 0.0824 ...
 $ M13   : num  0.1095 0.2119 0.1219 0.044 -0.0036 ...
 $ M14   : num  0.1527 0.0122 -0.1615 -0.0811 0.0575 ...
 $ M15   : num  0.0261 -0.5495 -0.0729 0.0964 0.0427 ...
 $ M16   : num  -0.2107 0.1518 -0.0696 0.0211 0.1104 ...
 $ M17   : num  -0.0196 -0.2409 0.0042 -0.0325 -0.0216 ...
 $ M18   : num  -0.2316 0.161 0.1239 0.181 0.0278 ...

import 函数假定 .txt 数据文件是以制表符分隔的。如果您有其他格式,可以在 format 参数中指定。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接