rconditional-statementsdata.tabler-colnames

How to use values of a column in defining bounds of another column values in R?


While processing my data (txt files), I need to define a lower and upper bound for my data in a way that values in between the bounds should remain as they are, else should become NA. I am using following code in R:

if ("K" %in% colnames(x)) {
    x[K < 900, K := NA]
  }

if ("WindQ" %in% colnames(x)) {
    x[Wind < 40 | Wind > ...., Wind := NA]
  }

The lower bound value is 40 The upper bound is defined by a formula i.e., 0.5 * Ba * K (to the power 1.2) + 150, where Ba and K are other columns present in the same txt file. The values of Ba and K varies in each text file.

The sample input of one text file is as follows:

Wind Pressure Ba K ....
110  289 50 1008 ....
....

I need to know how can I write the upper bound formula in the code. Could anyone please help.


Solution

  • one approach:

    library(dplyr)
    
    your_data_frame |>
      mutate(Wind = ifelse(Wind > 40 &
                           Wind < .5 * Ba * K^1.2,
                           Wind,
                           NA)
             )