-1

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.

1 Answers1

1

one approach:

library(dplyr)

your_data_frame |>
  mutate(Wind = ifelse(Wind > 40 &
                       Wind < .5 * Ba * K^1.2,
                       Wind,
                       NA)
         )
I_O
  • 4,983
  • 2
  • 2
  • 15
  • My data has Ba column written as Ba [W/s**2]. While running the code, I am getting an error i.e., Error: non-numeric argument to binary operator. I have tried the code as 0.5 * Ba * K^1.2, 0.5 * Ba [W/s**2] * K^1.2, 0.5 * "Ba" * K^1.2 or 0.5 * "Ba [W/s**2]" * K^1.2 but nothing is working. Could you please help. – Michael_Brun May 22 '23 at 17:34
  • 1
    You can enclose cumbersome variable names (e. g. containing blanks) with backticks (\``Ba [W/s2]\``) or convert them with `make.names()` or, better yet, choose legitimate variable names in the first place. See: https://stackoverflow.com/questions/59723243/r-use-make-names-to-rename-columns – I_O May 22 '23 at 18:17