I have a ridiculous problem: A factor simply changes its elements whenever I turn it into a column and use cbind
. The factor looks like this:
> bek_rec
[1] 4 3 4 2 2 3 4 4 3 3 5 3 4 4 3 4 4 4 4 3 3 4 2 3 4 4 4 3 4 4 4
[32] 4 3 3 3 4 4 4 4 3 3 3 4 4 3 4 4 4 4 3 4 4 3 3 4 4 4 4 3 4 5 5
[63] 3 3 4 3 3 3 (...)
As soon as I use cbind(bek_rec)
, it looks like this:
> cbind(bek_rec)
bek_rec
[1,] 3
[2,] 2
[3,] 3
[4,] 1
[5,] 1
[6,] 2
[7,] 3
[8,] 3
[9,] 2
[10,] 2
[11,] 4
[12,] 2
[13,] 3
[14,] 3
[15,] 2
[16,] 3
As you can see, these are not the same elements. The same thing happens when I cbind
it with other factors.
One the other hand, it does not happen for this factor:
> verw_rec
[1] 2 2 3 2 2 3 2 4 4 5 4 2 2 3 4 3 4 4 4 4 5 3 2 2 4 3 4 4 2 3 4
[32] 4 3 2 2 3 3
> cbind(verw_rec)
verw_rec
[1,] 2
[2,] 2
[3,] 3
[4,] 2
[5,] 2
[6,] 3
[7,] 2
[8,] 4
[9,] 4
[10,] 5
[11,] 4
[12,] 2
bek_rec
andverw_rec
are both factors (same class of objects)- no packages are loaded, but if they are (like
car
), same thing - problem happens with
cbind
, but not, for example, withsort(bek_rec)
- problem remains whether I reopen RStudio after saving the workspace and after not saving it.
My factor assignment for bek_rec
looks like this:
typmed3$bek_rec <- typmed$bek;
levels(typmed3$bek_rec) <- factor(c(2,3,3.5,4,4.5,5))
bek_rec <- factor(typmed$bek)
levels(bek_rec) <- factor(c(2,3,3.5,4,4.5,5))
levels(bek_rec) <- c("2","3","3","4","4","5"). #rounds .5s down.
The factor assignment for verw_rec
looks the same. I know it may be a bit redundant, but it did what it was supposed to.
Does anyone understand what's going on? I'm growing a bit desperate and am very thankful for any ideas!