Home > Software design >  Create factor for multiple dataframes using Loop function
Create factor for multiple dataframes using Loop function

Time:05-25

I have mutliple data frames (different size of rows, however, same columns). I want to create a factor in each of them using a loop.

list.dfs <- list(d1, d2, d3, d4, d5, d6, d7, d8)

for (i in 1:length(list.dfs)){
  d[i]$Gender <- factor(d[i]$Gender,
  levels = c(1, 2, 3),
  labels = c("female", "male", "divers")
)
}

This is not working

CodePudding user response:

R would not read d[i] as the object d1 if i = 1. You can access items of a list using [[i]]. Note that class(list.dfs[1]) is list while class(list.dfs[[1]]) is data.frame.

As an example

#example data
list.dfs <- list(structure(list(gender = c(1, 2, 3)), class = "data.frame", row.names = c(NA, 
-3L)), structure(list(gender = c(1, 2, 3)), class = "data.frame", row.names = c(NA, 
-3L)))

#check first item
list.dfs[[1]]

  gender
1      1
2      2
3      3

#use for loop to access all items of the list, apply function
for(i in 1:length(list.dfs)){
  list.dfs[[i]]$gender <- factor(list.dfs[[i]]$gender, levels = c(1, 2, 3),
                             labels = c("female", "male", "diverse"))

}

You also might want to read into lapply which applies a function on every object in the list.

#example using lapply
lapply(list.dfs, FUN = function(x) x$gender <- factor(x$gender, levels = c(1,2,3), labels = c("female", "male", "diverse")))

CodePudding user response:

Call your factor instruction in a lapply loop and assign the result back to list.dfs.

list.dfs <- list(structure(list(Gender = c(1, 2, 3)), 
                           class = "data.frame", row.names = c(NA, -3L)), 
                 structure(list(Gender = c(1, 2, 3)), 
                           class = "data.frame", row.names = c(NA, -3L)))

list.dfs <- lapply(list.dfs, \(x) {
  x$Gender <- factor(x$Gender, levels = c(1, 2, 3), labels = c("female", "male", "divers"))
  x
})

list.dfs
#> [[1]]
#>   Gender
#> 1 female
#> 2   male
#> 3 divers
#> 
#> [[2]]
#>   Gender
#> 1 female
#> 2   male
#> 3 divers

Created on 2022-05-24 by the reprex package (v2.0.1)

  • Related