# Ifelse, several variables and several conditions

For a university project, I have to set 4 logic conditions in order to do different operations, that is to say:
If an element from list1 (Latitude_Difference) is 0 && elements from list2 (Longitud_Difference) is 0, then operate: "element from list3 (Rumbo) = element from list3 + 180". Else evaluate the next condition
If an element from list1 is 0 && elements from list2 is > 0, then operate: " element from list3 = element from list3 -180". Else evaluate the next condition
If an element from list1 is > 0 && element from list2 is 0, then operate: element from list3 = 360 - element from list3". Else evaluate the next condition
If an element from list1 is > 0 && element from list2 is > 0, then operate: "element from list3 = element from list3". Else start again from the beginning
I attach a short reprex to illustrate my problem:

library(ggplot2)

data.frame(
Longitud_Difference = c(0.355555555555554, 2.03333333333333,
-2.98833333333334, -0.122222222222222,
-0.483055555555556, -0.0458333333333333, 0.218611111111111)
)
#>   Longitud_Difference
#> 1          0.35555556
#> 2          2.03333333
#> 3         -2.98833333
#> 4         -0.12222222
#> 5         -0.48305556
#> 6         -0.04583333
#> 7          0.21861111

data.frame(
Latitude_Difference = c(0.161666666666669, -1.57138888888889,
1.99666666666667, 0.0402777777777743,
0.172222222222224, 0.0633333333333326, -0.144999999999996)
)
#>   Latitude_Difference
#> 1          0.16166667
#> 2         -1.57138889
#> 3          1.99666667
#> 4          0.04027778
#> 5          0.17222222
#> 6          0.06333333
#> 7         -0.14500000

data.frame(
Rumbo = c(52.6522407924064, 52.1141691114194, 55.9611717385204,
62.9774520248583, 61.3077347299283, 24.8865879986665,
43.2327065129182)
)
#>      Rumbo
#> 1 52.65224
#> 2 52.11417
#> 3 55.96117
#> 4 62.97745
#> 5 61.30773
#> 6 24.88659
#> 7 43.23271

Rumbo <- function(RumboAbs,Longitud_Difference,Latitude_Difference){
declon = if_else(

condition = (Longitud_Difference<0 && Latitude_Difference<0),
true =  (RumboAbs = 180+RumboAbs),
false = RumboAbs,

condition = (Longitud_Difference<0 && Latitude_Difference>0),
true =  (360-RumboAbs),
false = RumboAbs,

condition = (Longitud_Difference>0 && Latitude_Difference>0),
true = (RumboAbs),
false = RumboAbs,

condition = (Longitud_Difference>0 && Latitude_Difference<0),
true = (180-RumboAbs),
false = RumboAbs
)

return(declon)}

RumboCircular<-
mutate(RumboCir = Rumbo(RumboAbs,Diferencia_Latitud,Diferencia_Longitud))

#Error in if_else(condition = (Longitud_Difference < 0 && Latitude_Difference <  :
#formal argument "condition" matched by multiple actual arguments

reprex::reprex()

I don't think your question is very clear, but is it something like this you're after?

require(tidyverse)

Longitud_Difference = c(0.355555555555554, 2.03333333333333,
-2.98833333333334, -0.122222222222222,
-0.483055555555556, -0.0458333333333333, 0.218611111111111)

Latitude_Difference = c(0.161666666666669, -1.57138888888889,
1.99666666666667, 0.0402777777777743,
0.172222222222224, 0.0633333333333326, -0.144999999999996)

Rumbo = c(52.6522407924064, 52.1141691114194, 55.9611717385204,
62.9774520248583, 61.3077347299283, 24.8865879986665,
43.2327065129182)

df <- data.frame(Rumbo, Latitude_Difference, Longitud_Difference)
rm(Rumbo, Latitude_Difference, Longitud_Difference)

Rum <- function(d) {
d %>%
mutate(
declon = case_when(
Longitud_Difference < 0 & Latitude_Difference < 0 ~ 180 + Rumbo,
Longitud_Difference < 0 & Latitude_Difference > 0 ~ 360 - Rumbo,
# Longitud_Difference > 0 & Latitude_Difference > 0 ~ Rumbo, # Unnecessary bc of last line
Longitud_Difference > 0 & Latitude_Difference < 0 ~ 180 - Rumbo,
TRUE ~ as.numeric(Rumbo)
)
)
}

Rum(df)
#>      Rumbo Latitude_Difference Longitud_Difference    declon
#> 1 52.65224          0.16166667          0.35555556  52.65224
#> 2 52.11417         -1.57138889          2.03333333 127.88583
#> 3 55.96117          1.99666667         -2.98833333 304.03883
#> 4 62.97745          0.04027778         -0.12222222 297.02255
#> 5 61.30773          0.17222222         -0.48305556 298.69227
#> 6 24.88659          0.06333333         -0.04583333 335.11341
#> 7 43.23271         -0.14500000          0.21861111 136.76729

reprex::reprex()
#> No input provided and clipboard is not available.
#> Rendering reprex...

Created on 2019-09-24 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.