Combn unclasses factor variables

UPDATE: FIXED

This is fixed in an upcoming release of R 3.1.0 . From CHANGELOG:

combn(x, simplify = TRUE) now gives a factorial result for inputting factors x (previously a user error).
Associated with PR # 15442


I just noticed a curious thing. Why combn appear to unlock factor variables for their base numeric values ​​for all but the first combination?

 x <- as.factor( letters[1:3] ) combn( x , 2 ) # [,1] [,2] [,3] #[1,] "a" "1" "2" #[2,] "b" "3" "3" 

This does not happen if x is a character:

 x <- as.character( letters[1:3] ) combn( x , 2 ) # [,1] [,2] [,3] #[1,] "a" "a" "b" #[2,] "b" "c" "c" 

Playability on R64 on OS X 10.7.5 and Windows 7.

+6
source share
3 answers

I think this is due to the conversion to matrix performed by the simplify parameter. If you do not use it, you will receive:

 combn( x , 2 , simplify=FALSE) [[1]] [1] ab Levels: abc [[2]] [1] ac Levels: abc [[3]] [1] bc Levels: abc 

The fact that the first column is OK is related to how combn works: the first column is set separately, and the remaining columns are then changed from the existing matrix using [<- . Consider:

 m <- matrix(x,3,3) m[,2] <- sample(x) m [,1] [,2] [,3] [1,] "a" "1" "a" [2,] "b" "3" "b" [3,] "c" "2" "c" 

I think the disturbing function is therefore [<- .

+2
source

As Conrad said, treatment of factors is often odd, or at least inconsistent. In this case, I find the behavior rather strange to create a mistake. Try sending it and see what the answer is.

Since the result is a matrix, and there is no matrix type of matrix, I think that the correct behavior would be to convert the input of the factor into a symbol somewhere near the beginning of the function.

+1
source

I had the same problem. It seems to force a return back to the character vector inside the combn command:

  > combn(as.character(x),2) [,1] [,2] [,3] [1,] "a" "a" "b" [2,] "b" "c" "c" 
0
source

Source: https://habr.com/ru/post/953159/


All Articles