[R] data.frame(1)*1:4 = 1?

fabian fabian at tbi.univie.ac.at
Thu Apr 3 08:56:07 CEST 2014


This is because your vector is recycled:

data.frame(1)*1:4 = data.frame(1)*c(1,2,3,4)

only the first element is needed since the data frame has nothing else 
to multiply with c(2,3,4)


(x<-data.frame(1:2, 3:4))
   X1.2 X3.4
1    1    3
2    2    4

(y<-x*5:7)

y[1,1] = x[1,1] * 5
y[2,1] = x[2,1] * 6
y[1,2] = x[1,2] * 7
y[2,2] = x[2,2] * 5

since you have e vector with length 3, for the 4th entry in the 
data.frame the first element in the vector is recycled.

hope this helps



On 03-04-2014 08:42, Spencer Graves wrote:
> Hello, All:
> 
> 
>       What's the logic behind "data.frame(1)*1:4" producing a scalar
> 1?  Or the following:
> 
> 
>  data.frame(1:2, 3:4)*5:7
>   X1.2 X3.4
> 1    5   21
> 2   12   20
> 
> 
>       I stumbled over this, because I thought I was multiplying a
> scalar times a vector, and obtaining a scalar rather than the
> anticipated vector.  I learned that my "scalar" was in fact a
> data.frame with one row and one column.
> 
> 
>       What am I missing?
> 
> 
>       Thanks,
>       Spencer
> 
> ______________________________________________
> 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