rloopsnames

How to loop through a list of data frames and assign the numeric portion of the name as a new column value for the data frame?


I have a list of data frames

list.files("data frame - 12345 - App", "data frame - 34567 - App", "data frame - 65849")

Each data frame looks like this

Col1|Col2 |Col3
Row1 Info1 NewInfo1
Row2 Info2 NewInfo2

I would like to add the number portion of the list name as the value for each row of each dataframe's new ID column. All three dataframes should like this

Col1|Col2 |Col3    |ID Name
Row1 Info1 NewInfo1 12345
Row2 Info2 NewInfo2 12345

Col1|Col2 |Col3    |ID Name
Row1 Info1 NewInfo1 34567
Row2 Info2 NewInfo2 34567

Col1|Col2 |Col3    |ID Name
Row1 Info1 NewInfo1 65849
Row2 Info2 NewInfo2 65849

Solution

  • assuming such a list of data.frames

    list(App_12345 =data.frame(a=letters[1:3], b=1:3),
         App_12345 =data.frame(a=letters[1:3], b=1:3))
    $App_12345
      a b
    1 a 1
    2 b 2
    3 c 3
    
    $App_12345
      a b
    1 a 1
    2 b 2
    3 c 3
    

    you can use

    library(tidyverse)
    list(App_12345 =data.frame(a=letters[1:3], b=1:3),
         App_34567 =data.frame(a=letters[1:3], b=1:3)) %>% 
      bind_rows(.id = "d") %>% 
      split(.$d)
    $App_12345
              d a b
    1 App_12345 a 1
    2 App_12345 b 2
    3 App_12345 c 3
    
    $App_34567
              d a b
    4 App_34567 a 1
    5 App_34567 b 2
    6 App_34567 c 3
    

    By edding a mutate before the split you can transform as appropriate

    mutate(d =gsub("App_", "", d)) 
    

    As an alternative you can use purrr's map function like

    list(App_12345 =data.frame(a=letters[1:3], b=1:3),
         App_34567 =data.frame(a=letters[1:3], b=1:3)) %>% 
    map2(., names(.), ~mutate(.x, d = unlist(str_extract_all(.y, "[0-9]+"))))