lundi 21 octobre 2019

if statement with three outcomes

I'd like to make a new column in which the value depends on other columns. There are three possible outcomes

  1. Distance < Min_disp = 0
  2. Distance < Max_disp = Distance
  3. Distance > Max_disp = Max_disp

I have tried using an if-statement, with multiple outcomes, but receive a warning.

Warning messages: 1: In if (Noord_2015_moved$Distance < Noord_2015_moved$Min_disp) { : the condition has length > 1 and only the first element will be used 2: In if (Noord_2015_moved$Distance < Noord_2015_moved$Max_disp) { : the condition has length > 1 and only the first element will be used

And indeed it only prints "Max_disp".

This is the code I've used

if (Noord_2015_moved$Distance < Noord_2015_moved$Min_disp) {
  0
} else if (Noord_2015_moved$Distance < Noord_2015_moved$Max_disp) {
  Noord_2015_moved$Distance
} else {
  Noord_2015_moved$Max_disp
}

I have also tried running it in three separate steps, but then I run into the problem that I don't know how to tell R to only apply part of the df$column, because now I get the error

number of items to replace is not a multiple of replacement length

Noord_2015_moved <- mutate(Noord_2015_moved, Actual_disp = ifelse(Distance < Min_disp, 0, NA))
Noord_2015_moved$Actual_disp[Noord_2015_moved$Distance < Noord_2015_moved$Max_disp] <- Noord_2015_moved$Distance
Noord_2015_moved$Actual_disp[is.na(Noord_2015$Actual_disp)] <- Noord_2015_moved$Max_disp

And this is my data

'data.frame':   301 obs. of  15 variables:
 $ Transmitter: Factor w/ 18 levels "A69-1601-22313",..: 1 1 1 1 1 1 1 2 2 2 ...
 $ Date       : Date, format: "2015-03-03" "2015-03-08" "2015-03-11" "2015-05-18" ...
 $ Date_time  : Factor w/ 279544 levels "1-03-15 0:00",..: 198302 258702 18684 85140 190788 182641 208718 26315 198759 205744 ...
 $ Receiver   : Factor w/ 17 levels "uitzetpunt 1-noord",..: 8 5 8 5 6 7 6 8 5 8 ...
 $ Station    : Factor w/ 17 levels "10","11","12",..: 15 12 15 12 13 14 13 15 12 15 ...
 $ Traject    : Factor w/ 53 levels "","10-10","10-9",..: 53 50 41 50 40 44 45 53 50 41 ...
 $ Interval   : num  83.4 12.7 42.6 25.2 217.4 ...
 $ Distance   : num  1540 6480 6480 6480 4690 4220 4220 1540 6480 6480 ...
 $ Min_speed  : num  0.02 0.51 0.15 0.26 0.02 0.73 0.52 0.01 0.02 0.02 ...
 $ Min_speed2 : num  0.00556 0.14167 0.04167 0.07222 0.00556 ...
 $ Length     : int  47 47 47 47 47 47 47 45 45 45 ...
 $ Activity   : chr  "Low" "Low" "Low" "Low" ...
 $ Moved      : chr  "Yes" "Yes" "Yes" "Yes" ...
 $ Min_disp   : num  160 4080 1200 2080 160 5840 4160 80 160 160 ...
 $ Max_disp   : num  240 6120 1800 3120 240 8760 6240 120 240 240 ...

Aucun commentaire:

Enregistrer un commentaire