I have 2 variables x and y. In X I have only 1 value with 1 level. I want to match levels of y to x. After matching levels, level's are matching but value of X is changing. Why this is so ?
x = as.factor(c(3))
> x
3
Levels: 3
y = as.factor(c(2,3,4))
> y
2 3 4
Levels: 2 3 4
Output -
levels(x) = levels(y)
print(x) 2
Levels: 2 3 4
The initial value of X was 3 now its 2.
I think this occurs because R presents the new level and not the value.
For example, if you will do as.numeric(x)
it will present 1 and not 3.
x <- as.factor(c(3))
as.numeric(x)
[1] 1
However, if you will unfactor the variable using varhandle::unfactor()
, it will present the "real" value.
varhandle::unfactor(x)
[1] 3
Thus, when you do levels(x) <- levels(y)
you don't relevel/refactor the levels of x to be like y - you adjusting/changing the levels and values.
x <- as.factor(c(3))
y <- as.factor(c(2,3,4))
levels(x) <- levels(y)
unfactor(x)
[1] 2
Doing thisx <- factor(x, levels = union(levels(x), levels(y)))
will solve your problem.
x <- as.factor(c(3))
y <- as.factor(c(2,3,4))
x
[1] 3 Levels: 3
x <- factor(x, levels = union(levels(x), levels(y)))
x
[1] 3 Levels: 2 3 4
unfactor(x)
[1] 3
Thank you @pieca for the comment.