Hi, I am very new to R. I am hoping to create formulas and assign them to locations within an array (or matrix, if it will work). Here's a simplified example of what I'm trying to do: form.arr <- array(31,5,3) for (i in seq(from=1, to=31, by=1)) { for (j in seq(from=1, to=5, by=1)) { form.arr[i,j,] <- as.formula(y~1+2) } } which results in this error: Error in form.arr[i, j, ] <- as.formula(y ~ 1 + 2) : incorrect number of subscripts The reason I had made the 3rd dimension of the array size 3 is because that's the length R tells me that formula is. When I had tried to do this using a matrix, using this code: form.mat <- matrix(31,5,3) for (i in seq(from=1, to=31, by=1)) { for (j in seq(from=1, to=5, by=1)) { form.mat[i,j] = as.formula(y~1+2) } } I was told: Error in form.mat[i, j] = as.formula(y ~ 1 + 2) : number of items to replace is not a multiple of replacement length My question is: is it possible to assign formulas within a matrix or array? If so, how? Thanks.not at real.com -- View this message in context: http://r.789695.n4.nabble.com/Assign-Formulas-to-Arrays-or-Matrices-tp2279136p2279136.html Sent from the R help mailing list archive at Nabble.com.
On 2010-07-06 1:13, McLovin wrote:> > Hi, > > I am very new to R. I am hoping to create formulas and assign them to > locations within an array (or matrix, if it will work). > > Here's a simplified example of what I'm trying to do: > > form.arr<- array(31,5,3) > for (i in seq(from=1, to=31, by=1)) { > for (j in seq(from=1, to=5, by=1)) { > form.arr[i,j,]<- as.formula(y~1+2) > } > } > > which results in this error: > Error in form.arr[i, j, ]<- as.formula(y ~ 1 + 2) : > incorrect number of subscripts > > The reason I had made the 3rd dimension of the array size 3 is because > that's the length R tells me that formula is. > > When I had tried to do this using a matrix, using this code: > > form.mat<- matrix(31,5,3) > for (i in seq(from=1, to=31, by=1)) { > for (j in seq(from=1, to=5, by=1)) { > form.mat[i,j] = as.formula(y~1+2) > } > } > > I was told: > > Error in form.mat[i, j] = as.formula(y ~ 1 + 2) : > number of items to replace is not a multiple of replacement length > > My question is: is it possible to assign formulas within a matrix or array? > If so, how? Thanks.not at real.comI don't think it's possible in the way you're trying to do it. A formula is not the same thing as a 3-element vector. Why not use a list? ps. for (i in seq(from=1, to=31, by=1)) is equivalent to for(i in seq_len(31)) -Peter Ehlers
Hi: See inline... On Tue, Jul 6, 2010 at 12:13 AM, McLovin <dave_decker@hotmail.com> wrote:> > Hi, > > I am very new to R. I am hoping to create formulas and assign them to > locations within an array (or matrix, if it will work). > > Here's a simplified example of what I'm trying to do: > > form.arr <- array(31,5,3) > for (i in seq(from=1, to=31, by=1)) { > for (j in seq(from=1, to=5, by=1)) { > form.arr[i,j,] <- as.formula(y~1+2) > } > } > > which results in this error: > Error in form.arr[i, j, ] <- as.formula(y ~ 1 + 2) : > incorrect number of subscripts >> fm <- y ~ 1 + 2 > class(fm)[1] "formula" # Let's investigate the fm object a little further...> str(fm)Class 'formula' length 3 y ~ 1 + 2 ..- attr(*, ".Environment")=<environment: R_GlobalEnv>> fm[1]`~`()> fm[2]y()> fm[3]1 + 2() The reason I had made the 3rd dimension of the array size 3 is because> that's the length R tells me that formula is. >True, but...> sapply(fm, class)[1] "name" "name" "call" so the classes of the components of the formula are not the same. An array requires that each element of the array have the same class. When I had tried to do this using a matrix, using this code:> > form.mat <- matrix(31,5,3) > for (i in seq(from=1, to=31, by=1)) { > for (j in seq(from=1, to=5, by=1)) { > form.mat[i,j] = as.formula(y~1+2) > } > } > > I was told: > > Error in form.mat[i, j] = as.formula(y ~ 1 + 2) : > number of items to replace is not a multiple of replacement length >You're trying to assign a three-element object to a single subscript of a matrix - R is (correctly) telling you that doesn't compute.> > My question is: is it possible to assign formulas within a matrix or > array?If so, how? Thanks.not@real.com>I think you might have better luck trying to assign formulas to list components, since the classes of the components of a formula are not the same. For this reason, you can't cram them into arrays or matrices for the reason given above. HTH, Dennis> -- > View this message in context: > http://r.789695.n4.nabble.com/Assign-Formulas-to-Arrays-or-Matrices-tp2279136p2279136.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
for assigning formulas to arrays use an array of list nr form.arr[[31,5]]y ~ 1 + 2 Jens Oehlschl?gel -----Urspr?ngliche Nachricht----- Von: McLovin Gesendet: Jul 6, 2010 9:13:49 AM An: r-help at r-project.org Betreff: [R] Assign Formulas to Arrays or Matrices?> >Hi, > >I am very new to R. I am hoping to create formulas and assign them to >locations within an array (or matrix, if it will work). > >Here's a simplified example of what I'm trying to do: > >form.arr for (i in seq(from=1, to=31, by=1)) { > for (j in seq(from=1, to=5, by=1)) { > form.arr[i,j,] } >} > >which results in this error: >Error in form.arr[i, j, ] incorrect number of subscripts > >The reason I had made the 3rd dimension of the array size 3 is because >that's the length R tells me that formula is. > >When I had tried to do this using a matrix, using this code: > >form.mat for (i in seq(from=1, to=31, by=1)) { > for (j in seq(from=1, to=5, by=1)) { > form.mat[i,j] = as.formula(y~1+2) > } >} > >I was told: > >Error in form.mat[i, j] = as.formula(y ~ 1 + 2) : > number of items to replace is not a multiple of replacement length > >My question is: is it possible to assign formulas within a matrix or array? >If so, how? Thanks.not at real.com >-- >View this message in context: http://r.789695.n4.nabble.com/Assign-Formulas-to-Arrays-or-Matrices-tp2279136p2279136.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >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.
On 06/07/2010 08:13, McLovin wrote:> > Hi, > > I am very new to R. I am hoping to create formulas and assign them to > locations within an array (or matrix, if it will work). > > Here's a simplified example of what I'm trying to do: > > form.arr<- array(31,5,3) > for (i in seq(from=1, to=31, by=1)) { > for (j in seq(from=1, to=5, by=1)) { > form.arr[i,j,]<- as.formula(y~1+2) > } > } > > which results in this error: > Error in form.arr[i, j, ]<- as.formula(y ~ 1 + 2) : > incorrect number of subscripts > > The reason I had made the 3rd dimension of the array size 3 is because > that's the length R tells me that formula is. > > When I had tried to do this using a matrix, using this code: > > form.mat<- matrix(31,5,3) > for (i in seq(from=1, to=31, by=1)) { > for (j in seq(from=1, to=5, by=1)) { > form.mat[i,j] = as.formula(y~1+2) > } > } > > I was told: > > Error in form.mat[i, j] = as.formula(y ~ 1 + 2) : > number of items to replace is not a multiple of replacement length > > My question is: is it possible to assign formulas within a matrix or array? > If so, how? Thanks.not at real.comYou can create a matrix of mode list to do that. An example is on page 39 of 'The R Inferno'. -- Patrick Burns pburns at pburns.seanet.com http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')