dimanche 23 avril 2017

How to Convert Columns using Loop in R

I intend to convert all the character columns into factors in R. Here is an example using diamonds dataset from ggplot2 package.

for (i in 1:length(diamonds)) {
  if (is.character(diamonds[, i])) {
    diamonds[,i] <- as.factor(diamonds[,i])
  }
}

I am confused why this code won't work. Could you please give me any adivce? Thanks

Aucun commentaire:

Enregistrer un commentaire