vendredi 14 septembre 2018

Function using conditions

I am trying to create a function that considers a determined number of samples (rivers) each one with a determined number of observations. Given 10 samples each one with 12 observations in a lognormal distribution with mean=4 and sd=1.4, I would like to obtain the number of times a particular number (6 - it refers to a standard number for water quality measurement) is counted. The following is the code for one experiment, considering "limit" as the maximum number of observations allowed to ovverpass 6.

set.seed(1001)
nobs<-12
limit<-round(0.10 * nobs, digits = 0) 
h2o <- as.data.frame(matrix(rnorm(10*12, mean = 4, sd = 1.4), ncol = 12))
paste(rep("Riv", nrow(h2o)), c(1:nrow(h2o)), sep = "")
rownames(h2o) <- paste(rep("Riv", nrow(h2o)), c(1:nrow(h2o)), sep = "")
colnames(h2o) <- paste(rep("Obs", ncol(h2o)), c(1:ncol(h2o)), sep = "")

#Number of rivers declared impared based in the assumptiom that the number of observations per river are 2 or more?

ifelse(h2o >=6,1,0)
h2o$Test<-rowSums(ifelse(h2o >=6,1,0))
length(h2o$Test[h2o$Test>1])

The function should resume the previous data and works for different observations with different samples.

Thanks

Aucun commentaire:

Enregistrer un commentaire