[R] Something wrong with my function Please Help
Jim Lemon
jim at bitwrit.com.au
Tue Sep 29 10:46:13 CEST 2009
On 09/29/2009 01:29 PM, Chunhao Tu wrote:
> Hi R users,
> I try to build a function to compute odds ratio and relative risk however
> something wrong. I stuck for many hours but I really don't know how to solve
> it. Would someone please give me a hint?
>
>
>> OR.RR<-function(x){
>>
> + x<- as.matrix(any(dim(x)==2))
> + OR<-(x[1,1]*x[2,2])/(x[1,2]*x[2,1])
> + RR<-(x[1,1]/(sum(x[1,])))/(x[2,1]/(sum(x[2,])))
> + return(OR);return(RR)
> + }
>
>> tt<-matrix(data=1:4,nrow=2,ncol=2)
>> OR.RR(tt)
>>
> Error in OR.RR(tt) : subscript out of bounds
>
Hi Tu,
The "any" function only returns a single logical value. Thus you have at
best a one element matrix. Your subscripts assume at least a four
element matrix. Maybe what you are trying to do is this:
OR.RR<-function(x) {
if(any(dim(x)>2) || length(dim(x) > 2)
stop("OR.RR only works with a 2x2 matrix")
...
Jim
More information about the R-help
mailing list