# data.frame(1)*1:4 = 1?

4 messages
Open this post in threaded view
|

## data.frame(1)*1:4 = 1?

 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 ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: data.frame(1)*1:4 = 1?

 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 > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.