Read a zip file in R from a subfolder
Asked Answered
S

1

6

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!

Smutchy answered 16/5, 2017 at 19:40 Comment(0)
B
4

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)
Breadbasket answered 16/5, 2017 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.