Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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|

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading