mardi 26 mars 2019

How to read in multiple files in a folder using paste in R

The function runs, but does not read the 104 files. It just gives me the years specified, and I need it to give me each of the year files so I can run further analysis. I have tried setting knitr as wel I am open to ideas about the inside of the function(year="all"), but my homewokr requires that we use the paste function

    setwd("C:/Users/Sarah/Downloads/names-full-datasets/names-full-datasets")
    library(purrr)
    baby.names <- function(year="all"){
    if(year!="all"){setwd("C:/Users/Sarah/Downloads/names-full-datasets/names- 
    full-datasets")
    cnames <- read.table(paste("yob",year,".txt", sep = ""),header = FALSE, 
    sep = ",", col.names = c("Name","Sex","Count"))
    cnames$year <- year
      }
     else
    {
    cnames <- lapply(1910:2013, baby.names)
    ldply(cnames)
    }    
    }
   yall <- baby.names()`
```

Aucun commentaire:

Enregistrer un commentaire