简体   繁体   中英

purrr::map_dfr gives number of list element as .id argument, not value of list element

I need to import a list of.xls files into R. Fairly standard operation, using file.list and purrr, done several times before. For some reason I cannot use readxl package as I keep getting libxls error, so switched to XLConnect, that seems to work.

However, using the following code:

file.list <- list.files('./Raw/', pattern = '.xls', full.names = TRUE)
rws <- function(x) {XLConnect::readWorksheetFromFile(x, sheet = 1, startRow =4)}
df <- purrr::map_dfr(file.list,rws, .id = "source")

I get an output, where source column includes position of the file in the list (1,2,3,...), not name of the file. What is the problem?

try to do it this way

file.list <- list.files('./Raw/', pattern = '.xls', full.names = TRUE) %>% 
          purrr::set_names()
rws <- function(x) {XLConnect::readWorksheetFromFile(x, sheet = 1, startRow =4)}
df <- purrr::map_dfr(file.list,rws, .id = "source")

You can get the name of the file from the position by -

library(dplyr)
library(purrr)

df <- map_dfr(file.list,rws, .id = "source") %>%
        mutate(source = basename(file.list)[source])
        #If you don't want the extension of the filename
        #mutate(source = tools::file_path_sans_ext(basename(file.list))[source])

df

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM