英文:
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 ("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
得分: 1
一种方法:
library(dplyr)
your_data_frame |>
mutate(Wind = ifelse(Wind > 40 &
Wind < .5 * Ba * K^1.2,
Wind,
NA)
)
英文:
one approach:
library(dplyr)
your_data_frame |>
mutate(Wind = ifelse(Wind > 40 &
Wind < .5 * Ba * K^1.2,
Wind,
NA)
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论