r - cbind factor vector with level names -
i create 3 vectors , try combine them 1 table using cbind. third vector factor 3 levels. output:
v1 <- c(1,2,3) v2 <- c(4,5,6) v3 <- factor(c('lorem','ipsum','dolor')) cbind(v1,v2,v3) v1 v2 v3 [1,] 1 4 3 [2,] 2 5 2 [3,] 3 6 1
preferred output be:
cbind(v1,v2,v3) v1 v2 v3 [1,] 1 4 lorem [2,] 2 5 ipsum [3,] 3 6 dolor
is possible level names instead of id display above?
not sure why need way. better use data.frame
can hold multiple classes. using cbind
, matrix
, matrix
can hold single class. so, if there single non-numeric value, columns transformed 'character' class.
cbind(v1, v2, v3=levels(v3)[v3]) # v1 v2 v3 #[1,] "1" "4" "lorem" #[2,] "2" "5" "ipsum" #[3,] "3" "6" "dolor"
or
cbind(v1, v2, v3=as.character(v3))
Comments
Post a Comment