Changing factor levels using for loop in R

dataexploration
r
data_wrangling

#1

my dataset has 60 variables, each variables has ordinal value with 5 levels (Agree, Disagree, Neutral, Strongly Agree, Strongly Disagree), but it is showing 6 levels with addition of level " ".

str(shopping$Felt_Unhappy)
Factor w/ 6 levels "","Agree","Disagree",..: 3 4 2 3 4 3 4 2 3 2 ...

I want to remove that unnecessary level from all the columns. I am using the code

for (i in colnames(shopping[,11:60])){   
    shopping$i <- factor(shopping$i)   
}

after executing the above code I am getting the following error

 Error in `$<-.data.frame`(`*tmp*`, "i", value = integer(0)) : 
 replacement has 0 rows, data has 162 

How to do this? Please help


#2

Hi @bhaskarjit

You can use droplevels function to remove unused levels as shown:

for(i in names(shopping[,11:60])){
        shopping[[i]] <- droplevels(shopping[[i]])
        shopping[[i]] <- factor(shopping[[i]], ordered=TRUE)      
}

Regards
Manish


#3

thanks @Manish,
it worked, but i have one more problem. I want my ordinal levels to have following numerical values
Strongly Agree = 5, Agree = 4, Neutral = 3, Disagree = 2, Strongly Disagree = 1
I am using ‘as.numeric’ function but the output is
Agree = 1, Disagree = 2, Neutral = 3, Strongly Agree = 4, Strongly Disagree = 5
how to manipulate this to get the desired number for each level?


#4

Hi @bhaskarjit

In such cases, the revalue function from plyr package does the trick. You can write as:

install.packages(plyr)
library(plyr)
variable_name <- revalue(variable_name, replace = c("Strongly Agree" = "5", "Agree" = "4", "Neutral" = "3", "Disagree" = "2", "Strongly Disagree" = "1"))

Regards
Manish


#5

Thanks @Manish, it worked :slight_smile: