Reputation: 377
I want to combine two columns with different length in one column with unique
factors, here is the example:
list1 <- as.factor(c('1a','2r','6t'))
list2 <- as.factor(c('1a','5p','3g','2341','7','2r'))
to
NewList
1a
2341
2r
3g
5p
6t
7
I have tried rbind
and data.frame
but they seem not working very well with columns in different length.
Upvotes: 3
Views: 136
Reputation: 12935
Using union
in base R:
data.frame(newlist=union(levels(list1), levels(list2)))
# newlist
#1 1a
#2 2r
#3 6t
#4 2341
#5 3g
#6 5p
#7 7
Upvotes: 3
Reputation: 1624
How about as.factor(unique(c(as.character(list1), as.character(list2))))
?
Upvotes: 2