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

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)

The data.table package, which extends the functionality of data frames in some very useful ways, will combine factors automatically when you use the rbindlist function. Plus, if your two data sets are large, it will usually combine them more quickly.

library(data.table)

# Example data:
# (If you already have data frames, you can convert them using `as.data.table(dataframename)`)
dataset1<-data.table(Number=1:2,Student=as.factor(c("Chris","Sarah")))
dataset2<-data.table(Number=1:2,Student=as.factor(c("Matt","Keith")))


# Combine the two data sets:
# (It's not necessary to convert factors to characters)
rbindlist(list(dataset1,dataset2))
#   Number Student
#1:      1   Chris
#2:      2   Sarah
#3:      1    Matt
#4:      2   Keith

There is interaction() function in the base R package. There is also strata() function in the survival package.

Tags:

R