简体   繁体   中英

How to create a column in multiple dataframes based on their names?

I have multiple data.frames named as below. Each of them has some data for each year (2018, 2019, 2020 and 2021):

subset_2018_empenhada
subset_2019_empenhada
subset_2020_empenhada
subset_2021_empenhada

I want to create a column in each of them named "ANO" and I need that the values of these columns for each data frame be the year in his name. For example: considering the data.frame "subset 2018_empenhada", the new column "ANO" must have the values 2018 in all rows

data ANO
64646464 2018
45454755 2018

Considering the "subset_2019_empenhada" | data| ANO | | -------- | ---- | | 34646| 2019| | 44785| 2019|

I know how to do that one by one, using data$ANO <- 2018 .

However, I sometimes work with a lot of data, and I believe doing all at once would be better and faster.

I though something with lapply , but I couldn't advance.

Any help? Thanks in advance!

We get the objects in a list with mget , loop over the list with imap and create the column by extracting the year part from the list names and if it needs to update the original objects, use list2env

library(dplyr)
library(purrr)
library(stringr)
mget(ls(pattern = "^subset_\\d{4}_empenhada$")) %>%
   imap(~ .x %>%   
               mutate(ANO = str_extract(.y, "\\d{4}"))) %>%
   list2env(.GlobalEnv)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM