Search code examples
rstringr

extract info from file name function stringr::str_split_n not exist


filename <-'Income Statement Dax 2020-03-31.xlsx'

I want to get the first string ('Income') and the third-string 'Dax' separately

according https://stringr.tidyverse.org/reference/str_split.html I can use function str_split_n() but have the error:

Error in str_split_n(filename, " ", 3) : could not find function "str_split_n"

Wondering is function stringr::str_split_n() still in use? if not, any suggestion I can use

stringr::str_split (filename, ' ')

combine with any another commend to get the first string ('Income') and the third-string 'Dax' separately?

I want to finally achieve:

     file<- 'Income'
           sheet <- 'Dax'
dmap <- readxl::read_excel(file, sheet = sheet ) %>% ungroup()

Thank you!


Solution

  • parts <- str_split(filename, " ")
    file <- parts[[1]][1]
    sheet <- parts[[1]][3]
    
    file
    sheet
    

    gives

    [1] "Income"
    [1] "Dax"