Domanda

This question is motivated by a bug filed here by Abiel Reinhart on data.table. I noticed that the same happens on data.frame as well.

Here's an example:

DF <- data.frame(x=1:5, y=6:10)
> DF*DF
   x   y
1  1  36
2  4  49
3  9  64
4 16  81
5 25 100

> class(DF*DF) # [1] "data.frame"

> DF^2
      x   y
[1,]  1  36
[2,]  4  49
[3,]  9  64
[4,] 16  81
[5,] 25 100

> class(DF^2) # [1] "matrix"

Why does "^" coerce it into a matrix? Any ideas? Note that ** is converted to ^ by the parser. So, doing DF**2 would give the same result as DF^2.

I don't find anything related to this coercion in ?`^`.

Edit: Neal's answer shows clearly the reason for ^ returning a matrix when operated on a data.frame. It'd be great if the question as to why ^ is being left out in that piece of code could be answered as well.

Edit 2: I also posted here on R-help and got a reply from Duncan that there seems to be no info reg. this change in the NEWS (admittedly, it's a quite old change as Joshua and Duncan also pointed out).

È stato utile?

Soluzione

Ops.data.frame implements the math operators for data frames as S3 generics, here is the last couple lines:

if (.Generic %in% c("+", "-", "*", "/", "%%", "%/%")) {
    names(value) <- cn
    data.frame(value, row.names = rn, check.names = FALSE, 
        check.rows = FALSE)
}
else matrix(unlist(value, recursive = FALSE, use.names = FALSE), 
    nrow = nr, dimnames = list(rn, cn))

So ^ gets returned as a matrix.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top