Search code examples
rurlunzip

Read a zip file in R from a subfolder


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!


Solution

  • 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)