[R] double if else

Thierry Onkelinx thierry.onkelinx at inbo.be
Mon Feb 23 08:56:17 CET 2015


Dear Daisy,

You are making things too complicated. There is no need for a loop and a
nested if structure.

areaKM <- 7500
dat <- data.frame(
  x = 1:30,
  y = 31:60,
  cellvalue = c(5, 6 , 7, rep(10, 10), rep(20, 17))
)#fake

if(areaKM > 15000){
  treshold <- 30
} else {
  treshold <- 5
}

n.cells <- table(dat$cellvalue)
names(n.cells)[n.cells >= treshold]

Best regards,

ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2015-02-23 4:35 GMT+01:00 Daisy Englert Duursma <daisy.duursma op gmail.com>:

> 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 op 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]]
>
> ______________________________________________
> R-help op r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]



More information about the R-help mailing list