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 change a certain columns in a list of data.frames to factor in r?

I have a list of data.frames, and want to change certain columns to a factor. The certain columns I want to change to a factor are c("station", "season"). I have tried various ways, but they did not work for me.

Any help please?

Here is a code for creating a data representing my dataset.

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

> df1 <- data.frame(station = c("MADA1", "MADA2", "MADA3", "MADA4", "MADA5"),
+                  rainfall = c(0, 5, 10, 15, 20),
+                  yield = c(2000, 3000, 4000, 5000, 6000),
+                  season = c('S1', 'S1', 'S2', 'S2', 'S1'))
> df2 <- df1
> df3 <- df1
> 
> list_1 <- list(df1, df2, df3)
> list_2 <- list(df1, df2, df3)
> mainlist <- list(list_1, list_2)
> 
> lapply(mainlist, head)
[[1]]
[[1]][[1]]
  station rainfall yield season
1   MADA1        0  2000     S1
2   MADA2        5  3000     S1
3   MADA3       10  4000     S2
4   MADA4       15  5000     S2
5   MADA5       20  6000     S1

[[1]][[2]]
  station rainfall yield season
1   MADA1        0  2000     S1
2   MADA2        5  3000     S1
3   MADA3       10  4000     S2
4   MADA4       15  5000     S2
5   MADA5       20  6000     S1

[[1]][[3]]
  station rainfall yield season
1   MADA1        0  2000     S1
2   MADA2        5  3000     S1
3   MADA3       10  4000     S2
4   MADA4       15  5000     S2
5   MADA5       20  6000     S1


[[2]]
[[2]][[1]]
  station rainfall yield season
1   MADA1        0  2000     S1
2   MADA2        5  3000     S1
3   MADA3       10  4000     S2
4   MADA4       15  5000     S2
5   MADA5       20  6000     S1

[[2]][[2]]
  station rainfall yield season
1   MADA1        0  2000     S1
2   MADA2        5  3000     S1
3   MADA3       10  4000     S2
4   MADA4       15  5000     S2
5   MADA5       20  6000     S1

[[2]][[3]]
  station rainfall yield season
1   MADA1        0  2000     S1
2   MADA2        5  3000     S1
3   MADA3       10  4000     S2
4   MADA4       15  5000     S2
5   MADA5       20  6000     S1


>Solution :

A purrr approach (depth to get to the second list "layer"):

mainlist %>% map_depth(., 2,~.x %>% mutate(across(c("station", "season"), as.factor)))
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