Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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

like image 776
user2792124 Avatar asked Oct 05 '13 19:10

user2792124


2 Answers

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

like image 105
Viktor Avatar answered Oct 11 '22 11:10

Viktor


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)
like image 40
Ricardo Saporta Avatar answered Oct 11 '22 12:10

Ricardo Saporta