1

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!

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

What if I have data.frames with other things besides "empenhado"? For example: subset_2018_empenhada subset_2019_liquidado subset_2020_liquidado subset_2021_empenhada
@DaniloImbimbo if it is not specific you can change the pattern to pattern = "^subset_\\d{4}_.*"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.