I have a large data frame that has a lot of string values that I want to clean.
Example:
students <- data.frame(name = c("John", "Jerry", "Bill", "Tom", "Mary", "Bill"),
class = c("A", "B", "-", "#NA", "A", "low"), stringsAsFactors = FALSE)
I want every student who is not in class A,B or C to be set to D. My current solution is:
'%!in%' <- function(x,y)!('%in%'(x,y))
for(i in 1:nrow(students)) {
if(students$class[i] %!in% c("A", "B", "C")) {
students$class[i] <- "D"
}
}
Is there a better solution than this, preferably with piping as there are a number of columns like this?
Thanks!