In below mlist
object, how does one update the value to 'character'
for all elements which currently are c("ordered", "factor")
?
library(tidyverse)
mlist <- sapply(diamonds,class)
mlist[['color']] <- 'character'
can just update one of them. Is there any way to update all of them at once?
mlist[['color']] <- 'character'
Here is the quickest way that I can think of:
# Positions where `mlist` has the value `c("ordered", "factor").
pos <- which(sapply(mlist, function(x) identical(x, c("ordered", "factor"))))
# Change these
mlist[pos] <- "character"
# Result
mlist
Note that this doesn't change the class variable of your data set but merely the values is the list mlist
that you generated.