[R] double if else

Daisy Englert Duursma daisy.duursma at gmail.com
Mon Feb 23 04:35:01 CET 2015


The corrected version is below. I found an error.


areaKM<-7500 #test for 7500 and 30000

dat<-data.frame(x=1:30,y=31:60,cellvalue=c(5,6,7,rep(10,10),rep(20,17)))#fake
data
cells<-unique(as.numeric(dat$cellvalue))#find unique cellvalues
pres100km <- list() #empty list

for (ii in 1:length(cells)){ #make loop to find 100km cells with at least
30% of 10km cells being within the alpha hull,
  #or if area of alpha hull less that 15000km2 keep all 100km2 cells where
points fall
  subdat<-subset(dat,cellvalue ==cells[ii])
  if (areaKM >= 15000) {
    if (nrow(subdat) < 30) {
      next
    } else {
      pres100km[[ii]]<-cells[ii] #100kmcells >= 30% coverage of 10kmcells
    }
  } else {
    if(nrow(subdat) < 5) {
      next
    } else {
      pres100km[[ii]]<-cells[ii] #more than 5 obs
    }
  }
}



On Mon, Feb 23, 2015 at 1:54 PM, Daisy Englert Duursma <
daisy.duursma at gmail.com> wrote:

> Hello,
> I need to make a double clause in an if else/ next statement. I wrote a
> single one fine but I could not make it work for two statements statements.
>
> Please see the following example:
>
>
> #Goals
>   #1)if size of area is greater than 15000 test that there are a minimum
> of nrow() = 30,
>     #write those with at least 30 values to list
>   #2) if less than 15000, write those with >= 5 rows
>
> # in the following example I should have a list where one has a value of
> 10 and the other 20.
>
>
> areaKM <- 7500 #test for 7500 and 30000, at 30000 should have empty list
> dat<-data.frame(x=1:30,y=31:60,cellvalue=c(5,6,7,rep(10,10),rep(20,17)))#fake
> data
> cells<-unique(as.numeric(dat$cellvalue))#find unique cellvalues
> pres100km <- list() #empty list
>
> #make loop to find dat$cellsvalue with at least 30 rows or if areaKM is
> less than15000  keep cellvalues for those with more than 5 rows.
> for (ii in 1:length(cells)){
>   subdat<-subset(dat,cellvalue ==cells[ii])
>   if (areaKM >- 15000) {
>     if (nrow(subdat) < 30) {
>       next
>     } else {
>       pres100km[[ii]]<-cells[ii] #100kmcells >= 30% coverage of 10kmcells
>     }
>   } else {
>     if (nrow(subddat) < 5) {
>       next
>     } else {
>       pres100km[[ii]]<-cells[ii] #100kmcells >= 50% coverage of 10kmcells
>     }
>   }
> }
>
>
>
>
>
>
>
> --
> Daisy Englert Duursma
> Department of Biological Sciences
> Room W19F 135
> Macquarie University, North Ryde, NSW 2109
> Australia
>
> Tel +61 2 9850 1302
>
>
>


-- 
Daisy Englert Duursma
Department of Biological Sciences
Room W19F 135
Macquarie University, North Ryde, NSW 2109
Australia

Tel +61 2 9850 1302

	[[alternative HTML version deleted]]



More information about the R-help mailing list