From pburns at pburns.seanet.com  Sat Dec 13 11:48:23 2008
From: pburns at pburns.seanet.com (Patrick Burns)
Date: Sat, 13 Dec 2008 10:48:23 +0000
Subject: [Rd] [R] odd behavior when multiplying data frame by an element
In-Reply-To: <1102425641.17145511229133138174.JavaMail.javamailuser@localhost>
References: <1102425641.17145511229133138174.JavaMail.javamailuser@localhost>
Message-ID: <494392F7.9000808@pburns.seanet.com>

I think it might be up for discussion whether or not
there is a bug in here somewhere.

 > mat10 <- matrix(1:6, 3)
 > mat10 * 1:12
Error: dims [product 6] do not match the length of object [12]
 > data.frame(mat10) * 1:12
  X1 X2
1  1 16
2  4 25
3  9 36
 > version
               
_                                                              
platform       
i386-pc-mingw32                                                
arch           
i386                                                           
os             
mingw32                                                        
system         i386, 
mingw32                                                  
status         Under development 
(unstable)                                   
major          
2                                                              
minor          
9.0                                                            
year           
2008                                                           
month          
11                                                             
day            
22                                                             
svn rev        
47006                                                          
language       
R                                                              
version.string R version 2.9.0 Under development (unstable) (2008-11-22 
r47006)


It seems dangerous to me that there is not at least
a warning in the data frame case.  I think I'd prefer
an error like the matrix case.

Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

markleeds at verizon.net wrote:
> could someone explain what is happening below ? I was trying to solve 
> a related question on the list and then , as I was solving it,
> I was getting strange answers and then I noticed below. It's obviously 
> not a bug but I don't get it. Thanks.
>
> m <- data.frame(class = c("birds", "planes"), feather = c(1,3), jet = 
> c(2,4))
> d1 <- data.frame(jet = c(10), feather = c(20))
>
> print(m)
> m[,-1]*2       # THIS IS FINE
> m[,"jet"]*2     # THIS IS FINE
>
>
> print(d1["jet"]) # THIS IS FINE
> m[,"jet"]*d1["jet"] # THIS IS ODD
>
> ______________________________________________
> 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.
>
>



[Rd] [R] odd behavior when multiplying data frame by an element
Patrick Burns