jeudi 21 novembre 2019

How can we make if statement in R evaluate a vector of logical values without using ifelse()

I am trying to generate a summary for each column in a dataset with a function. The main idea is below

function(col){
##if numeric
##return class, percentage of NAs, percentage of zeros etc

##else if column's values contain TRUE or FALSE
  else if(toupper(unique(col)) %in% c('TRUE', 'FALSE'))
##return class="logical", percentage of NAs, percentage of zeros etc

##else if character & not contain TRUE or FALSE
##return class, percentage of NAs, percentage of zeros etc

##else
##return class="Date",percentage of NAs, percentage of zeros etc
}

However, the part

else if(toupper(unique(col)) %in% c('TRUE', 'FALSE'))

gives back an error

the condition has lengh>1 and only the first value will be used

To my understanding, why we are having this error is that

(toupper(unique(col))

gives back a vector of logical values and if statement can only evaluate a single TRUEor FALSE. Only the first row will be evaluated. For my purpose, only evaluating the first row is enough, but I wonder how should we rewrite this so R understands that I want to know if the whole column contains any TRUEorFALSE?

Aucun commentaire:

Enregistrer un commentaire