jeudi 30 juin 2016

How to check if data table has empty rows?

I am writing function that filters out some random data from a data table random according to an id value ,but I want it first to check if there is actual data rows in the data table first. I ended up writing an if statement and using is.null but its still for the condition but its not working and ends up accessing the else statement and then giving me an error. The code is posted below, please help me

new.filterID <- function(DataTable,id) {
  if(DataTable == is.null){
    return(print("No Data Available: ",id))
  } else { filtered <- subset(DataTable, ID == id)
    return(aggregate(Value ~ YEAR_WW, filtered, mean))
  }
}
filteredData <- new.filterID(random, 213)

The error I get when I run this is

Error in aggregate.data.frame(mf[1L], mf[-1L], FUN = FUN, ...) : 
  no rows to aggregate 

Also below the empty data table random

Value   YEAR_WW

Aucun commentaire:

Enregistrer un commentaire