[R] On "^" returning a matrix when operated on a data.frame

Duncan Murdoch murdoch.duncan at gmail.com
Thu Nov 14 01:17:23 CET 2013

On 13-11-13 6:52 PM, Arunkumar Srinivasan wrote:
> Duncan,
> Thank you. What I meant was that "^" is the only *arithmetic operator*
> to result in a matrix on operating in a data.frame. I understand it's
> quite old code. Also, your explanation makes sense, with the exception
> of "/" operator, I suppose (I could be wrong here).

You're right, "/", "%/%" and "%%" also return consistent types.  So my 
explanation is wrong.  The NEWS entry for this change appears to be in 
the 0.63 release,

     o	Ops.data.frame :  things like  d.fr < a	  now return a matrix

That doesn't give much of a hint for why "^" is handled differently than 

Duncan Murdoch

> Arun
> On Thursday, November 14, 2013 at 12:32 AM, Duncan Murdoch wrote:
>> It's not just ^ that is missing, the logical relations like <, ==, etc
>> also return matrices. This is very old code (I think from 1999), but I
>> would guess that the reason is that the ^ and < operators always return
>> values of a single type (numeric and logical respectively), whereas the
>> other operators can take mixed type inputs and return mixed type outputs.
>> Duncan Murdoch
>>> Please let me know if I should be posting this to R-devel list instead.
>>> Thank you very much,
>>> Arun
>>> [[alternative HTML version deleted]]
>>> ______________________________________________
>>> R-help at r-project.org <mailto:R-help at r-project.org> mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list