[R] assigning 'default' values

Duncan Murdoch murdoch at stats.uwo.ca
Mon Feb 1 19:03:07 CET 2010


On 01/02/2010 12:33 PM, Richard Thompson wrote:
> Hi all,
>
> probably something really simple, that I've missed but I'm running this loop
> in my Rscript:
>
> <code>
> for (i in 1:nrow(cells)){
>   if(plate == as.character(cells[i,1])){
>      plate2 <-cells[i,2]}
>
> }
> <code>
>
> it's assigning the relevant data correctly as long as plate exists somewhere
> in cells[i,1]. The problem is that if it doesn't exist then it retains the
> previous value of plate2. I've tried setting it to a default value in a
> number of ways, but most have destroyed the data structure of plate2.
> Anybody know how I can give it a value where the if loop has failed without
> loosing the remainder of plate2?


The usual way would be a loop like this:

plate2 <- default

for (i in 1:nrow(cells)){
  if(plate == as.character(cells[i,1])){
     plate2 <-cells[i,2]}

}


Duncan Murdoch



More information about the R-help mailing list