Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a CSV from github into R

I am trying to read a CSV from github into R:

latent.growth.data <- read.csv("https://github.com/aronlindberg/latent_growth_classes/blob/master/LGC_data.csv") 

However, this gives me:

Error in file(file, "rt") : cannot open the connection In addition: Warning message: In file(file, "rt") : unsupported URL scheme 

I tried ?read.csv, ?download.file, getURL (which only returned strange HTML), as well as the data import manual, but still cannot understand how to make it work.

What am I doing wrong?

like image 571
histelheim Avatar asked Jan 21 '13 15:01

histelheim


People also ask

How do I import a csv file from GitHub into R?

Downloading CSV's from Github is simple, copy the web address from the csv raw file extension and read it in R. The Github repo and data selected is the COVID-19 data from John Hopkins University's Center for Systems Sciences and Engineering. The data can be found here: covid_19_daily_reports/11-24-2020.

How do I pull data from GitHub to R?

The most direct way to get data from Github to your computer/ into R, is to download the repository. That is, click the big green button: The big, green button saying “Clone or download”, click it and choose “download zip”. Of course, for those using Git and Github, it would be appropriate to clone the repository.

How do I read a csv file in R?

To load a. csv file into the current script and operate with it, use the read. csv() method in base R. The output is delivered as a data frame, with row numbers given to integers starting at 1.


1 Answers

Try this:

library(RCurl) x <- getURL("https://raw.github.com/aronlindberg/latent_growth_classes/master/LGC_data.csv") y <- read.csv(text = x) 

You have two problems:

  1. You're not linking to the "raw" text file, but Github's display version (visit the URL for https:\raw.github.com....csv to see the difference between the raw version and the display version).
  2. https is a problem for R in many cases, so you need to use a package like RCurl to get around it. In some cases (not with Github, though) you can simply replace https with http and things work out, so you can always try that out first, but I find using RCurl reliable and not too much extra typing.
like image 112
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 01 '22 10:10

A5C1D2H2I1M1N2O1R2T1