Re: [R] Lists and Binary Operators.

From: Prof Brian Ripley <>
Date: Wed 19 Oct 2005 - 15:53:45 EST

On Wed, 19 Oct 2005, Narcyz Ghinea wrote:

> Dear R Users,
> Any insights into why the following occurs would be helpful....
> Firstly:
> #Evaluating proportions
> p<-as.list(rep(0,times=length(n))) #creating object of appropriate size.
> for(j in 1:length(n)){
> for(k in 1:length(n[[j]])){
> p[[j]][[k]]<- (s[[j]][[k]]/n[[j]][k])}} # 31 x k x num_samples(dim)
> where k varies
> The list object s[[j]][[k]] has 3 levels, j, k, and i. j represents the
> review number, k the study number in that review, and i is the number of
> simulations in that study. n[[j]][k] is also a list object with 2
> levels that represents the number of patients in study k of review j.
> When I run this the following error message occurs:
> Error: more elements supplied than there are to replace.
> However, if I make the simple adjustment p[[j]][[k]]<-
> list(s[[j]][[k]]/n[[j]][k]) it works fine! If anyone knows why please
> let me know. I couldn't get the answer from reading the "list" help.

Hmm. Take a look at p: it is a list of numerical items of length 1, and you are trying to replace p[[j]][[k]], a single element in a numeric vector, by a numeric vector of length > 1.

I think you need to set p up properly, most likely as a copy of s.

> Secondly:
> #Evaluating weights.
> w<-as.list(rep(0,times=length(n))) #creating object of appropriate size.
> for(j in 1:length(n)){
> for(k in 1:length(n[[j]])){
> w[[j]][[k]]<- (1/(p[[j]][[k]]*n[[j]][k]*(1-p[[j]][[k]])))}} # 31 x k x
> num_samples(dim) where k varies
> When I run this section of the code, if gives the same error message as
> the previous one. When I try to solve it using the "list" function
> again, instead it gives a different error message:
> Error in p[[j]][[k]] * n[[j]][k] : non-numeric argument to binary
> operator
> The error message is straightforward but what I can't understand is why
> an error trying to calculate p[[j]][[k]]*n[[j]][k] in the second case,
> but no such problem evaluating s[[j]][[k]]/n[[j]][k] in the first case,
> even though they are objects of the same type and sizes?

Have you verified that claim? I think p[[j]][[k]] is a list, which is what you assigned there, and s[[j]][[k]] is a numeric vector.

Compare str(p) to str(s).

> How can I get rid of the second error? I have tried converting the
> p[[j]][[k]] vector into the numeric type but it won't allow it.

Had you quoted the error message your mistake would have been much more obvious. I suspect it says

     (list) object cannot be coerced to 'double'

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
PLEASE do read the posting guide!
Received on Wed Oct 19 15:58:41 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 19:05:58 EST