0

I have a list of 10 dataframes, on which I want to apply several functions. I am quite new to R. I searched on the internet but can't find the solution.

The dataframes all have the same variables. I want to set the data type of one variable as a factor (which is now integer), having two levels: wetland and no wetland (1 or 0) in every dataframe. This variable is in the third column.

I tried something like this but I can't find the right code:

factorcol = "WETLAND"
datalist[, factorcol] <- lapply(datalist[,factor_cols], factor)
datalist[, factorcol] <- lapply (levels (datalist[,factorcol]))<-c("no wetl","wetl")

Or should I use as.factor which I would use if I treated a dataframe seperately? And how I apply it to third column of all dataframes in the list?

Sotos
  • 51,121
  • 6
  • 32
  • 66
Lies
  • 71
  • 9
  • Try something like `datalist <- lapply(datalist, function(i){i$WETLAND <- as.factor(i$WETLAND, ...); i})` – Sotos Jul 30 '20 at 09:05

2 Answers2

0

If you have to apply this to only one column, you don't need lapply to iterate over columns. Use lapply to iterate over dataframes and change the 3rd column to factor specifying labels and levels.

datalist <- lapply(datalist, function(x) {
              x[[3]] <- factor(x[[3]], levels = c(0, 1), 
                               labels = c("no wetl","wetl"))
              x
             })
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

Since you know the column you want to convert to factor, you can simply do,

datalist <- lapply(datalist, function(i){i$WETLAND <- as.factor(i$WETLAND, ...); i})
Sotos
  • 51,121
  • 6
  • 32
  • 66