mercredi 28 octobre 2015

If then statement in R based on number of rows

I have an equation that I want to apply to a list of files. This equation makes a calculation based on water depth and density. I have data every 1 meter for all files. This equation works when the water is greater than 30m (sheet has more than 30 rows). How do I incorporate an if then statement to tell R to plug in "0" or "NA" if there are equal to or less than 30 rows in some files?

Here is my working code for files with more than 30 rows.

stratindex2=function(file){
    ctd2=read.csv(file)         
    ctd2=ctd2[-c(1:30),]
    x=ctd2$Density..sigma.t..kg.m.3..
    row=nrow(ctd2)              
    (x[row]-x[1])/(row-1)           
}

Aucun commentaire:

Enregistrer un commentaire