[R] indexing??

Petr Savicky savicky at cs.cas.cz
Tue Feb 28 19:59:28 CET 2012


On Tue, Feb 28, 2012 at 08:50:45AM -0800, helin_susam wrote:
> Dear Petr Pikal and Petr Savicky thank you for your replies..
> 
> If the y vector contains different elements my algorithm gives this result;
> y <- c(1,2,3,4,5,6,7,8,9,10) 
> x <- c(1,0,0,1,1,0,0,1,1,0) 
> 
> n <- length(x) 
> 
> t <- matrix(cbind(y,x), ncol=2) 
> 
> z = x+y 
> 
> for(j in 1:length(x)) { 
> out <- vector("list", ) 
> 
> for(i in 1:10) { 
> 
> t.s <- t[sample(n,n,replace=T),] 
> 
> y.s <- t.s[,1] 
> x.s <- t.s[,2] 
> 
> z.s <- y.s+x.s 
> 
> out[[i]] <- list(ff <- (z.s), finding=any (y.s==y[j])) 
> kk <- sapply(out, function(x) {x$finding}) 
> ff <- out[! kk] 
> } 
> }

Hi.

It is hard to debug a code, which we do not understand.
Both me and Petr Pikal expressed objections against your
code. It would help us to reply your question, if you take
our objections and suggestions into account or explain,
what we do not understand well.

Can you comment on the suggestions from the previous emails?

I would like to add one more. Why do you use

  ff <- (z.s)

inside

  out[[i]] <- list(ff <- (z.s), finding=any (y.s==y[j]))

?

This expression includes the value into the list, but not
under the name ff and rewrites the global variable ff instead.

If you want to include (z.s) as a component named as ff, then use 

  list(ff = (z.s), finding...

Petr Savicky.



More information about the R-help mailing list