3

I have a data in which I would like to import in R.

One way is to download the data from gist and then use

read.delim 
read.table

but I am more searching to find other ways instead downloading it and then use the above functions

Here is an example data:

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

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    `library(httr) ; read.table(text=content(GET("https://gist.githubusercontent.com/anonymous/2c69ab500bfa94d0268a/raw/cdaedc27897d4e570f61317711c3f548430570eb/example.txt"), as="text"), header=TRUE)` – hrbrmstr Feb 25 '15 at 10:43
  • 1
    `df <- read.table("https://gist.githubusercontent.com/anonymous/2c69ab500bfa94d0268a/raw/cdaedc27897d4e570f61317711c3f548430570eb/example.txt", header=TRUE, stringsAsFactors=FALSE, sep="") ` worked for me – Khashaa Feb 25 '15 at 12:00
  • 2
    @Khasha that's a Windows-only solution – hrbrmstr Feb 25 '15 at 13:55

2 Answers2

4

you can use something like this

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

I've been working on a package to do just this. It's called rio and is available on GitHub. Once it's installed, you can do this in one line:

# 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")

Result:

> 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 ...

The import function assumes .txt data files are tab-separated. If you have another format, you can specify it in the format argument.

Thomas
  • 43,637
  • 12
  • 109
  • 140