I do get the following error message: *Error in lookup.svc[i, j] <- svc[svc$st == unique(svc$st)[i] & svc$vc == : * * replacement has length zero*
I also thought it might be because of how R treats NA, but then I would expect the loop to stop at the place of error (i=1, j=2) and not continue to fill out all of column i. I've tried using %in%, but that seems to do a non-positional check for whether or not entries are *somewhere *in those vectors. I need to find the location of where the match occurs. My goal is to turn this: st vc y A Z .2 B Z .4 B Y .3 C Y .1 C X .8 into a 2x2 table with entries 'y' vc Z Y X A .2 0 0 st B .4 .3 0 C 0 .1 .8 Right now it's giving me vc Z Y X A .2 0.000 0.000 st B 0 0 0 C 0 0 0 So it seems to finish out the row that it's currently on, but then won't continue to loop. -Robin On Sun, Apr 25, 2010 at 4:44 PM, Peter Alspach < peter.alsp...@plantandfood.co.nz> wrote: > Tena koe Robin > > Do you get an error or warning? > > It may have something to do with how == treats NA: > > x <- 1:4 > x[x == 1] > [1] 1 > x <- c(1:4, NA) > x[x == 1] > [1] 1 NA > x[x %in% 1] > [1] 1 > > If so, using %in% is one way to avoid the problem. However, I would > have thought you'd get an error message if this were the case. > > HTH ..... > > Peter Alspach > > > -----Original Message----- > > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > > project.org] On Behalf Of Robin Jeffries > > Sent: Monday, 26 April 2010 10:26 a.m. > > To: r-help@r-project.org > > Subject: [R] Obvious reason for not looping twice? > > > > Is there an obvious reason why this won't loop to i=2 and beyond? > > There are many combinations of *st* & *vc* that don't exist in svc. > For > > example, when s=1 there's only an entry at v=1. That's fine, the > entry > > can > > stay 0. > > > > lookup.svc <- > > array(0,dim=c(length(unique(svc$st)),length(unique(svc$vc))), > > dimnames=list(unique(svc$st), unique(svc$vc))) > > > > for (i in 1:length(unique(svc$st))) { > > for (j in 1:length(unique(svc$vc))){ > > lookup.svc[i,j] <- svc[svc$st == unique(svc$st)[i] & svc$vc == > > unique(svc$vc)[j], 4] > > }} > > > > > > Thanks, > > Robin > > > > ~~~~~~~~~~~~~~~~~~~ > > -Robin Jeffries > > Dr.P.H. Candidate > > UCLA School of Public Health > > rjeffr...@ucla.edu > > 530-624-0428 > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide http://www.R-project.org/posting- > > guide.html > > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.