如何在R中使用一列的值来定义另一列的边界值?

huangapple go评论72阅读模式
英文:

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

问题

以下是您要翻译的代码部分:

在处理我的数据(txt文件)时,我需要以一种方式定义数据的下限和上限,以便介于这些限制之间的值应保持不变,否则应变为NA。我正在R中使用以下代码:

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

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

下限值为40
上限由以下公式定义,即0.5 * Ba * K(1.2次方)+ 150,其中Ba和K是同一txt文件中存在的其他列。Ba和K的值在每个文本文件中都不同。

一个文本文件的示例输入如下:

风压 Ba K ....
110 289 50 1008 ....
....

我需要知道如何在代码中编写上限公式。请问有谁可以帮助?

如果您需要进一步的帮助或有其他问题,请随时提问。

英文:

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 (&quot;K&quot; %in% colnames(x)) {
    x[K &lt; 900, K := NA]
  }

if (&quot;WindQ&quot; %in% colnames(x)) {
    x[Wind &lt; 40 | Wind &gt; ...., 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

得分: 1

一种方法:

library(dplyr)

your_data_frame |&gt;
  mutate(Wind = ifelse(Wind &gt; 40 &amp;
                       Wind &lt; .5 * Ba * K^1.2,
                       Wind,
                       NA)
         )
英文:

one approach:

library(dplyr)

your_data_frame |&gt;
  mutate(Wind = ifelse(Wind &gt; 40 &amp;
                       Wind &lt; .5 * Ba * K^1.2,
                       Wind,
                       NA)
         )

huangapple
  • 本文由 发表于 2023年5月22日 03:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76301636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定