Skip to content
Prev 87034 / 398502 Next

unused factor levels in reshape

When reshaping a dataframe in which there are unused factor levels in the id variable, I get the following error:

Error in if (!all(really.constant)) warning(gettextf("some constant variables (%s) are really varying",  : 
        missing value where TRUE/FALSE needed

For example,
defines a dataframe, and a subframe with some unused factor levels (i = 4, 5). Then
i x y.1 y.2
1 1 0  13   6
3 2 0  12   5
5 3 0  10   9
7 4 1   9  11
9 5 1  12   8

works fine but
Error in if (!all(really.constant)) warning(gettextf("some constant variables (%s) are really varying",  : 
        missing value where TRUE/FALSE needed

produces the error, which happens during the check to see if the variables assumed constant are constant. The problem is that reshape searches over all the levels of the id variable (i in this case) to see if the other variables (here x) are constant. But there is no x associated with i = 4, 5 in the smaller dataframe, so
1    2    3    4    5 
TRUE TRUE TRUE   NA   NA

produces some NAs. A slight change to the reshape code to work around this problem would be to use (the equivalent of)
1    2    3 
TRUE TRUE TRUE

in the reshapeWide function within reshape, but perhaps there is a good reason not to do this?

Daniel