[R] Assign Formulas to Arrays or Matrices?

Peter Ehlers ehlers at ucalgary.ca
Tue Jul 6 10:13:39 CEST 2010


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.com

I 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



More information about the R-help mailing list