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!
CodePudding user response:
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)