Replace Values in Factor Vector or Column in R (Example) | as.character, as.factor & levels Function

preview_player
Показать описание
R code of this video:

x <- factor(c(LETTERS[1:5], "C")) # Create example factor vector
x # Print example factor vector

x_warning <- x # Duplicate example vector
x_warning[3] <- "New" # Try to replace factor value

x_warning # Print factor with NA

x_new1 <- x # Duplicate example vector
x_new1[3] <- "New" # Replace specific value
x_new1 # Print updated vector

x_new2 <- x # Duplicate example vector
levels(x_new2)[levels(x_new2) == "C"] <- "New" # Replace entire factor level
x_new2 # Print updated vector

x2 = factor(c("aaa", "bbb", "aaa")))
data # Print example data frame

data_new <- data # Duplicate example data frame

data_new$x1[1] <- "Hello" # Replace specific value

levels(data_new$x2)[levels(data_new$x2) == "aaa"] <- "Hey" # Replace entire factor level

data_new # Print new data frame

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

Bravo, Joachim!!!! The tutorial was excellent!!!

agsoutas
Автор

Hello Joachim, I'm new in R and your videos have helped me a lot. Right now I'm struggling with some dbl in a column and I need to convert them into chr to display them in my graph, to see the different code users in the point graph and not represented as a heat map

hugolozano