user2792124 user2792124 - 1 month ago 16
R Question

How to combine two columns of factors into one column without changing the factor levels into number

I am trying to find a way to combine two columns of factors into one column without changing the factor levels into numbers. For instance, consider the following two data.frame datasets

dataset 1 dataset 2
Number Student Number Student
1 Chris 1 Matt
2 Sarah 2 Keith


I am trying to take "student" column from the dataset1 and the "student" column from the dataset2, and make one big student column containing the names "Chris", "Sarah", "Matt", and "Keith"

I tried:

student.list<-c(dataset1[,2],dataset2[,2])
student.list


However, this doesn't work since the names turns into numbers with c() function. I want my list to preserve the names of students (i.e. without converting them into numbers). I also tried cbind(), but gives same problem as c()...

Thank you

Answer

factors are numbers that happen to have labels. When you combine factors, you generally are combining their numeric values. This can often trip a person up.

If you want their labels, you must coerce them to strings, using as.character

 student.list <- c( as.character(dataset1[,2]) ,
                    as.character(dataset2[,2])  )

If you want to get that back to factors, wrap it all in as.factor (can be all in one line, or split into two lines for easier reading)

 student.list <- c(as.character(dataset1[,2]),as.character(dataset2[,2]))
 student.list <- as.factor(student.list)