Dear useRs, Here is a weird behavior of transform function: mtcars1<-matcars transform(mtcars1,t1=3,t2=4) Error in data.frame(`_data`, e[!matched]) : arguments imply differing number of rows: 32, 1 instead, this works: mtcars1$t1<-0 transform(mtcars1,t1=3,t2=4) also works if applied in turn: transform(mtcars1,t1=3) transform(mtcars1,t2=4) I often need to use this for creating new variables in data frame from those already present. Sorely needed!! Best, Vitalie.
Try: cbind(mtcars, t1 = 3, t2 = 4) On Tue, Dec 2, 2008 at 11:14 AM, Vitalie Spinu <vitosmail at rambler.ru> wrote:> Dear useRs, > > Here is a weird behavior of transform function: > > mtcars1<-matcars > transform(mtcars1,t1=3,t2=4) > Error in data.frame(`_data`, e[!matched]) : > arguments imply differing number of rows: 32, 1 > > instead, this works: > > mtcars1$t1<-0 > transform(mtcars1,t1=3,t2=4) > > also works if applied in turn: > > transform(mtcars1,t1=3) > transform(mtcars1,t2=4) > > I often need to use this for creating new variables in data frame from those > already present. > Sorely needed!! > > Best, > Vitalie. > > ______________________________________________ > 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. >
As the help page says If some of the values are not vectors of the appropriate length, you deserve whatever you get! So you can use mtcars1 <- mtcars mtcars1[c("t1", "t2")] <- cbind(rep(3,32), rep(4, 32)) or even mtcars1 <- transform(mtcars, t1=rep(3, 32), t2=rep(4, 32)) Vitalie Spinu wrote:> Dear useRs, > > Here is a weird behavior of transform function: > > mtcars1<-matcars > transform(mtcars1,t1=3,t2=4) > Error in data.frame(`_data`, e[!matched]) : > arguments imply differing number of rows: 32, 1 > > instead, this works: > > mtcars1$t1<-0 > transform(mtcars1,t1=3,t2=4) > > also works if applied in turn: > > transform(mtcars1,t1=3) > transform(mtcars1,t2=4)'works'? Only if you assign the result.> I often need to use this for creating new variables in data frame from > those already present. > Sorely needed!!Just learn to use indexing: transform() is just syntactic sugar that you are not making use of.> Best, > Vitalie.-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Tue, 02 Dec 2008 17:37:44 +0100, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:> As the help page says > > If some of the values are not vectors of the appropriate length, > you deserve whatever you get! > > So you can use > > mtcars1 <- mtcars > mtcars1[c("t1", "t2")] <- cbind(rep(3,32), rep(4, 32)) >Ok..I got it, it is an usual pain with R: vectors with length 1 are recycled and data.frames with nrows=1 and arrays with dim[1]=1 are not. Will have to use mtcars[c("t1","t2")]<-with(mtcars, cbind(t1=..., t2=...)) or rewrite transform.data.frame myself. Thanks a lot, Vitalie.
On Tue, 2 Dec 2008, hadley wickham wrote:>> The underlying issue is actually not in transform() but in data.frame(): >> >>> aq <- airquality[sample(1:153,6),] >>> data.frame(aq, list(a=1,b=2)) >> Error in data.frame(aq, list(a = 1, b = 2)) : >> arguments imply differing number of rows: 6, 1 >>> data.frame(aq, list(a=1)) >> Ozone Solar.R Wind Temp Month Day a >> 3 12 149 12.6 74 5 3 1 >> 31 37 279 7.4 76 5 31 1 >> 34 NA 242 16.1 67 6 3 1 >> 65 NA 101 10.9 84 7 4 1 >> 59 NA 98 11.5 80 6 28 1 >> 133 24 259 9.7 73 9 10 1 >> > > Is this a bug or a "feature"?As documented: Objects passed to data.frame should have the same number of rows, but atomic vectors, factors and character vectors protected by I will be recycled a whole number of times if necessary. How did you manage to miss that in the help page?> > Hadley > > -- > http://had.co.nz/ >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595