Home > Software engineering >  Change values depending of values in other columns [R]
Change values depending of values in other columns [R]

Time:09-20

I would like to change a value in one column if two conditions depending of values in other columns are meet. i.e. when the values of df$StationID is "LaKo-.10" and the value of df$Depth interval is "400-1000", I would like the df$max depth become 1000 instead of 0.

I have tried the given code, but every values of the max depth is changed. And not only the one selected by the conditions :

df$`max depth` <- df$`max depth`[df$StationID == "LaKo2018-.10" & df$`Depth interval` == '400-1000'] <- 1000

The given dataset :

df <- structure(list(StationID = c("LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.10", "LaKo2018-.1", "LaKo2018-.10", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.10", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.10", "LaKo2018-.1", "LaKo2018-.1", 
"LaKo2018-.1", "LaKo2018-.10", "LaKo2018-.1", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.1", 
"LaKo2018-.10", "LaKo2018-.1", "LaKo2018-.1", "LaKo2018-.10", 
"LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10", "LaKo2018-.10"
), `Depth interval` = c("0-25", "0-25", "0-25", "0-25", "0-25", 
"0-25", "0-25", "0-25", "0-25", "0-25", "0-25", "0-25", "0-25", 
"0-25", "0-25", "0-25", "0-25", "0-25", "0-25", "0-25", "0-25", 
"0-25", "0-25", "0-25", "0-25", "0-25", "0-25", "0-25", "0-25", 
"0-25", "0-50", "0-50", "0-50", "0-50", "0-50", "0-50", "0-50", 
"0-50", "0-50", "0-50", "0-50", "0-50", "0-50", "0-50", "0-50", 
"0-50", "0-50", "0-50", "0-50", "0-50", "0-50", "0-50", "0-50", 
"0-50", "0-50", "0-50", "0-50", "0-50", "0-50", "0-50", "100-175", 
"100-175", "100-175", "100-175", "100-175", "100-175", "100-175", 
"100-175", "100-175", "100-175", "100-175", "100-175", "100-175", 
"100-175", "100-175", "100-175", "100-175", "100-175", "100-175", 
"100-175", "100-175", "100-175", "100-175", "100-175", "100-175", 
"100-175", "100-175", "100-175", "100-175", "100-175", "100-200", 
"100-200", "100-200", "100-200", "100-200", "100-200", "100-200", 
"100-200", "100-200", "100-200", "100-200", "100-200", "100-200", 
"100-200", "100-200", "100-200", "100-200", "100-200", "100-200", 
"100-200", "100-200", "100-200", "100-200", "100-200", "100-200", 
"100-200", "100-200", "100-200", "100-200", "100-200", "200-400", 
"200-400", "200-400", "200-400", "200-400", "200-400", "200-400", 
"200-400", "200-400", "200-400", "200-400", "200-400", "200-400", 
"200-400", "200-400", "200-400", "200-400", "200-400", "200-400", 
"200-400", "200-400", "200-400", "200-400", "200-400", "200-400", 
"200-400", "200-400", "200-400", "200-400", "200-400", "25-50", 
"25-50", "25-50", "25-50", "25-50", "25-50", "25-50", "25-50", 
"25-50", "25-50", "25-50", "25-50", "25-50", "25-50", "25-50", 
"25-50", "25-50", "25-50", "25-50", "25-50", "25-50", "25-50", 
"25-50", "25-50", "25-50", "25-50", "25-50", "25-50", "25-50", 
"25-50", "400-1000", "400-1000", "400-1000", "400-1000", "400-1000", 
"400-1000", "400-1000", "400-1000", "400-1000", "400-1000", "400-1000", 
"400-1000", "400-1000", "400-1000", "400-1000", "400-1000", "400-1000", 
"400-1000", "400-1000", "400-1000", "400-1000", "400-1000", "400-1000", 
"400-1000", "400-1000", "400-1000", "400-1000", "400-1000", "400-1000", 
"400-1000", "50-100", "50-100", "50-100", "50-100", "50-100", 
"50-100", "50-100", "50-100", "50-100", "50-100", "50-100", "50-100", 
"50-100", "50-100", "50-100", "50-100", "50-100", "50-100", "50-100", 
"50-100", "50-100", "50-100", "50-100", "50-100", "50-100", "50-100", 
"50-100", "50-100", "50-100", "50-100"), `max depth` = c(25, 
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 175, 175, 175, 175, 
175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 
175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 
200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 
200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 
200, 200, 200, 200, 400, 400, 400, 400, 400, 400, 400, 400, 400, 
400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 
400, 400, 400, 400, 400, 400, 400, 400, 50, 50, 50, 50, 50, 50, 
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 
50, 50, 50, 50, 50, 50, 50, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
100, 100, 100, 100)), row.names = c(NA, -240L), class = c("tbl_df", 
"tbl", "data.frame"))

CodePudding user response:

As stefan said in the comments, don't lead your expression with df$`max depth` <- use just

df$`max depth`[df$StationID == "LaKo2018-.10" & df$`Depth interval` == '400-1000'] <- 1000

Why? Two reasons:

  1. Assignment operations are chain operators, meaning that

    a <- b <- 3
    

    will assign the value 3 to both b and a. This happens because the default "return value" of an assignment is the value being assigned, though it is returned "invisibly". You can force it to be seen by wrapping the expression in (..), such as

    B <- 3
    (B <- 3)
    # [1] 3
    
  2. However, it is just the value assigned, not the resulting value in the LHS. For instance, demonstrating on mtcars:

    mtcars$cyl
    #  [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
    (mtcars$cyl <- 4L)
    # [1] 4
    

    Despite the fact that the LHS mtcars$cyl is length 32, the return value from the assignment is length 1 because that's what the RHS (4L) is.

    Continuing this example with mtcars, let's change all gear values of 5 to 99:

    mtcars$gear == 5
    #  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
    # [22] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
    (mtcars$gear[mtcars$gear == 5] <- 99)
    # [1] 99
    

    There are 5 instances of gear == 5, and the return value from the assignment is still length 1.

    Similar to how you did it, though would be

    mtcars$gear <- (mtcars$gear[mtcars$gear == 5] <- 99)
    

    Knowing that the first assignment <- 99 is returned to be assigned to the next LHS mtcars$gear <-, it then assigns the value 99 to all of $gear.

Since the first assignment df$`max depth`[...] <- 1000 already updated the values where the specific conditions were met, there is no need to do any more assignment.

As a demonstrative alternative (that I don't suggest is better, just different), one could have done

df$`max depth` <- ifelse(
  df$StationID == "LaKo2018-.10" & df$`Depth interval` == '400-1000',
  1000,
  df$`max depth`
)

which assigns a new vector to the entire column `maxdepth`, where the values in that new vector are either 1000 or the original value, depending on the conditional.

  •  Tags:  
  • r
  • Related