Home > database >  How to take a substring of dataframes' names inside a list to replace all values in a column wi
How to take a substring of dataframes' names inside a list to replace all values in a column wi

Time:03-29

I have a long list of data frames. I want to get the last two strings of each data frame's name and use it to replace all values in certain column of each data frame within the list.

Toy example:

df_stringPA <- data.frame(name="MU",
                  col1= c(1,2,3),
                  col2= c(1,2,3))

df_stringCA <- data.frame(name="MU",
                  col1= c(1,2,3),
                  col2= c(1,2,3))

test_list <- list(PA=df_PA, CA=df_CA)

### desired output:

output_df_PA <- data.frame(name="PA",
                    col1= c(1,2,3),
                    col2= c(1,2,3))


output_df_CA <- data.frame(name="CA",
                    col1= c(1,2,3),
                    col2= c(1,2,3))

If I do it for an individual dataframe within the list, it works:

### doing it separately for each df within the list:
test_list$PA$name <- str_sub(deparse(substitute(test_list$PA)), start= -2)

But it is a long list, so doing it manually is not the best option. I have tested:

test_list <- lapply(test_list, function(x) x$name <- str_sub(deparse(substitute(x)), start= -2))

But I get the data frames within the list replaced by ']]'

CodePudding user response:

You can use Map:

 Map(function(a, b) {a$name <- b; a}, a = test_list, b = names(test_list))
$PA
  name col1 col2
1   PA    1    1
2   PA    2    2
3   PA    3    3

$CA
  name col1 col2
1   CA    1    1
2   CA    2    2
3   CA    3    3

CodePudding user response:

We could use imap

library(purrr)
library(dplyr)
imap(test_list, ~ .x %>%
     mutate(name = .y))
$PA
  name col1 col2
1   PA    1    1
2   PA    2    2
3   PA    3    3

$CA
  name col1 col2
1   CA    1    1
2   CA    2    2
3   CA    3    3
  • Related