1

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!

Sherece
  • 13
  • 3
  • great first question. when you ask future questions please be sure to create a small [reproducible example] (https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – greengrass62 Jun 10 '21 at 15:45

2 Answers2

0

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
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

If you only want to extract the numbers from the names in your list, you could use this str_extract(names(list.data), "\\-*\\d+\\.*\\d*"). Note that names(list) returns the data frame column names, not the name of the data frames.