# [R] Subscript out of bounds- (my) problem with expansion of expression?

Jan_Svatos@eurotel.cz Jan_Svatos at eurotel.cz
Fri Apr 6 10:41:04 CEST 2001

```Hi,
I have this problem (R 1.2.2 on Pentium, OS WinNT4 SP6) :

mytable
is a 3D (n*p*2) table of  non-negative "integers".
There is a need to do n times

chisq.test (rbind(mytable[i,,1], mytable[i,,2])) #on p*2 tables

Columns with zero value in the first row should be omitted, for example

chisq.test (rbind(mytable[i,-c(3,4,6),1], mytable[i,-c(3,4,6),2]))  #for
i-th table with zero values at [1,3] , [1,4]and [1,6]

First row,
>mytable[1,,1]
1   2   3   4   5   6   7   8
187  95  0   0   6  0  60  88

Second row,
>mytable[1,,2]
1   2   3   4   5   6   7   8
146  50   0   2  11   0  36 109

My question is: Why the upper call of chisq.test works,
but application of function (described below) generating the "-c(3,4,6)"
string automatically from the table does not?

A function generating logical vector indicating non-zero values in a vector
"vec1"

pom1 <- function(vec1)
{
po1  <-(vec1)>0
po1
}

>pom1(mytable[1, ,1])
1    2    3    4    5    6    7    8
TRUE TRUE   NA   NA TRUE   NA TRUE TRUE

A function "generator" with argument of logical vector "vec" indicating
non-presence of zeros is defined, which returns desired string:

generator <- function(vec)
{
tempor <- NULL
for(j in 1:length(vec))
{
if (!vec[j]) tempor<-c(tempor, j)
}

generat <- paste (tempor, collapse=",")
generat <- paste ("-c(",generat,")" , sep="")
generat
}

> generator(pom1(mytable[1, ,1]))
[1] "-c(3,4,6)"

or

>pom <- pom1(mytable[1, ,1])
> generator(pom)
[1] "-c(3,4,6)"

Then the "generator" is applied inside chisq.test:

for (i in 1:length(mytable[,1,1]))
{
pom <- pom1(mytable[i,,1])
mychi2[i]<-chisq.test(rbind(mytable[i,generator(pom),1],
mytable[i,generator(pom),2]))\$p.value
}

(or just for  mytable[,1,1]).

and as a result, an "Error: subscript out of bounds" is given immediately.
Why?
Neither the use of  noquote(generator(pom)) instead of generator(pom) does
help, nor eval(generator(pom)).

Therefore: How could I "force" R to think of generator(pom) as of -c(3,4,6)
(or something similar)
inside the call of chisq.test (or, of course, another function)?

I think this should be (easily) possible, but I don't know how.

Thank you for any help,
Jan

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```