Search code examples
rlistdataframesapply

How can I extract numbers from dataframe names in a list in R?


I have a list called list.data with over 600 dataframes in it. Each data frame has a unique name e.g. 4dMU6_20080605tp.txt and within the name is a date "20080605" that I want to extract and add to a new column in each dataframe.

I have created the new column in each dataframe with the column title "Date" but now need to extract the numbers from multiple dataframe names- any idea how I could do this?

I've tried sapply but presumably it doesn't work as it is searching the dataframes as oppossed to the dataframe names.

sapply(list.data, function(x){as.numeric(x[8])})

Any help would be greatly appreciated!


Solution

  • Using lapply, we can add new Date column to each data frame in the list, using gsub to obtain the date from the name of each list element.

    lst_names <- names(list.data)
    list.data <- lapply(lst_names, function(x) {
        list.data[[x]]$Date <- gsub("^.*_|[A-Za-z]*\\.\\w+$", "", x)
        return(list.data[[x]])
    })
    names(list.data) <- lst_names