vendredi 26 juin 2020

Function with multiple conditions if, else or ifelse

I have one function that create a new column with dial phases (day, dawn, night and dusk) at data frame, but apparently the conditions they are overlapping each other.

dialphase <- function(df) {
  #loading the necessary package
  require(oce)
  
  #getting the sun angle from local time, longitude and latitude by the correction for atmospheric refraction
  x2<-sunAngle(df$localtime, df$lon, df$lon, useRefraction = TRUE)
  
  #converting into data.frame
  x3<-do.call(rbind, lapply(x2$altitude, as.data.frame))
  
  #creating new colum with the sun angles
  df$sun<- x3$`X[[i]]`
  
  #creating new column with only by hour about local time to do the next conditions
  df$hourBRT <- as.POSIXlt(df$localtime)$hour
  df <- df[!is.na(df$hourBRT),]
  
  #creating new column with dial phases by sun angles and hours with day, dawn, night and dusk
  df$dial_phase[df$sun >= 6.0] <- "day" 
  
  df$dial_phase[df$sun  < 6.0 & df$sun > -12.0 & df$hourBRT > 3] <- "dawn" #manhã
  
  df$dial_phase[df$sun <= -12.0] <- "night"
  
  df$dial_phase[df$sun < 6.0 & df$sun > -12.0 & df$hourBRT > 16 ] <- "dusk" #noite
  
  df$hourBRT<-NULL
  return(df)
}

And my guess is because the function gonna work right using if, else or ifelse, im correct? I dont have abilities with do function using these codes. Someone can help, please?

I tryed this things , but didnt create anything:


#1
t<-function(df) {
  #loading the necessary package
  require(oce)
  
  #getting the sun angle from local time, longitude and latitude by the correction for atmospheric refraction
  x2<-sunAngle(df$localtime, df$lon, df$lon, useRefraction = TRUE)
  
  #converting into data.frame
  x3<-do.call(rbind, lapply(x2$altitude, as.data.frame))
  
  #creating new colum with the sun angles
  df$sun<- x3$`X[[i]]`
  
  #creating new column with only by hour about local time to do the next conditions
  df$hourBRT <- as.POSIXlt(df$localtime)$hour
  df <- df[!is.na(df$hourBRT),]
  
  with(df, ifelse(df$dial_phase[df$sun >= 6.0], df$dial_phase =="day", 
                  ifelse(df$dial_phase[df$sun  < 6.0 & df$sun > -12.0 & df$hourBRT > 3], df$dial_phase =="dawn", 
                         ifelse(df$dial_phase[df$sun <= -12.0], df$dial_phase =="night",
                                ifelse(df$dial_phase[df$sun < 6.0 & df$sun > -12.0 & df$hourBRT > 16 ], df$dial_phase =="dusk")))))
  return(df)
}


# 2
t<-function(df) {
  #loading the necessary package
  require(oce)
  
  #getting the sun angle from local time, longitude and latitude by the correction for atmospheric refraction
  x2<-sunAngle(df$localtime, df$lon, df$lon, useRefraction = TRUE)
  
  #converting into data.frame
  x3<-do.call(rbind, lapply(x2$altitude, as.data.frame))
  
  #creating new colum with the sun angles
  df$sun<- x3$`X[[i]]`
  
  #creating new column with only by hour about local time to do the next conditions
  df$hourBRT <- as.POSIXlt(df$localtime)$hour
  df <- df[!is.na(df$hourBRT),]
  
  with(df, ifelse(df$dial_phase[df$sun >= 6.0], "day", 
                  ifelse(df$dial_phase[df$sun  < 6.0 & df$sun > -12.0 & df$hourBRT > 3], "dawn", 
                         ifelse(df$dial_phase[df$sun <= -12.0], "night",
                                ifelse(df$dial_phase[df$sun < 6.0 & df$sun > -12.0 & df$hourBRT > 16 ], "dusk")))))
  return(df)
}

Thank you!

Aucun commentaire:

Enregistrer un commentaire