Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a zip file in R from a subfolder

Tags:

I have been ripping my hair out with this. I am trying to run the following:

temp <- tempfile()
download.file("http://seanlahman.com/files/database/baseballdatabank-2017.1.zip", temp, mode="wb")
table1 <- unz(temp, "Salaries.csv")
salaries <- read.csv(table1, sep=",", header=T)

However, I think it is not working because the actual file I want (Salaries) is in a folder called 'core' - I looked at the structure by downloading the zipped file to my computer. How can I add something to this code to look in the core folder and get the Salaries data? I want to do this directly from the URL if possible. Thanks!

like image 886
JassiL Avatar asked May 16 '17 19:05

JassiL


1 Answers

You can explicitly specify the path within the archive file:

temp <- tempfile()
download.file("http://seanlahman.com/files/database/baseballdatabank-2017.1.zip", temp, mode="wb")
table1 <- unz(temp, "baseballdatabank-2017.1/core/Salaries.csv")
salaries <- read.csv(table1, sep=",", header=T)
like image 136
M-- Avatar answered Sep 25 '22 10:09

M--