I have leveled a factor variable in R. Once I did it, I am not able to see the original values behind each label. One example is given below:
library(Hmisc)
x <- as.factor(c("", "1", "2", "3", "4", "", "1", "2", "3", "4"))
x1 <- factor(x, levels = c("", "1","2", "3", "4"), labels = c("NA", "A", "B", "C", "D"))
table(x)
x
1 2 3 4
2 2 2 2 2
table(x1)
x1
NA A B C D
2 2 2 2 2
One factor variable x
is created here and labeled in x1
. From the table of x1
, I don't get to see original values (0, 1, 2, 3, 4) of x1
. To have a table of original values, I need to rely on table(x)
. Also, function table(as.integer(x1))
does not fetch original values from x1
since factor does not consider blank cells as 0. For few variables, creating similar factor variables is fine like here. But not possible when I work with large number of variables. Is there any function where I can see original values in a factor as well as factor labels from the same varibale?