Search code examples
rmergedataframelevels

Merging objects of class "factor" on R


I have four data.frames (DATA1, DATA2, DATA3 and DATA4)

I'm interested in accessing one particular variable, which is common for every data.frame: DATA1$Point DATA2$Point DATA3$Point DATA4$Point

class(DATA1$Point)
[1] "factor"`

I want to merge the levels of each data.frame object in a single object

Something like:

TOTAL_POINTS <- merge(DATA1$Point,DATA2$Point,DATA3$Point,DATA4$Point)

But, obviously, it doesn't work.

I wanna know which Points were visited, taking in consideration all of the data.frames objects.

How can I do it? Thanks in advance!


Solution

  • merge would be the wrrong function. Just use factor( unlist(. )), but only after converting the individual factor vectors to character first since they may have different levels attributes:

     TOTAL_POINTS<-factor(  unlist ( lapply( list( DATA1$Point,
                                            DATA2$Point,
                                            DATA3$Point,
                                            DATA4$Point) ,
                                      as.character)
                          ) )