有条件地替换 data.frame 中的值 [英] Conditional replacement of values in a data.frame
问题描述
我试图了解如何在不使用循环的情况下有条件地替换数据框中的值.我的数据框结构如下:
I am trying to understand how to conditional replace values in a dataframe without using a loop. My data frame is structured as follows:
> df
a b est
1 11.77000 2 0
2 10.90000 3 0
3 10.32000 2 0
4 10.96000 0 0
5 9.90600 0 0
6 10.70000 0 0
7 11.43000 1 0
8 11.41000 2 0
9 10.48512 4 0
10 11.19000 0 0
和 dput
输出是这样的:
structure(list(a = c(11.77, 10.9, 10.32, 10.96, 9.906, 10.7,
11.43, 11.41, 10.48512, 11.19), b = c(2, 3, 2, 0, 0, 0, 1, 2,
4, 0), est = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("a",
"b", "est"), row.names = c(NA, -10L), class = "data.frame")
我想做的是检查b
的值.如果 b
为 0,我想将 est
设置为 a
中的值.我知道 df$est[df$b == 0] <- 23
会将 est
的所有值设置为 23,当 b==0代码>.我不明白的是如何在该条件为真时将
est
设置为 a
的值.例如:
What I want to do, is to check the value of b
. If b
is 0, I want to set est
to a value from a
. I understand that df$est[df$b == 0] <- 23
will set all values of est
to 23, when b==0
. What I don't understand is how to set est
to a value of a
when that condition is true. For example:
df$est[df$b == 0] <- (df$a - 5)/2.533
给出以下警告:
Warning message:
In df$est[df$b == 0] <- (df$a - 5)/2.533 :
number of items to replace is not a multiple of replacement length
有没有办法可以传递相关单元格而不是向量?
Is there a way that I can pass the relevant cell, rather than vector?
推荐答案
既然你是有条件地索引 df$est
,你还需要有条件地索引替换向量 df$a
代码>:
Since you are conditionally indexing df$est
, you also need to conditionally index the replacement vector df$a
:
index <- df$b == 0
df$est[index] <- (df$a[index] - 5)/2.533
当然,变量 index
只是临时的,我用它来使代码更易读.一步就可以写出来:
Of course, the variable index
is just temporary, and I use it to make the code a bit more readible. You can write it in one step:
df$est[df$b == 0] <- (df$a[df$b == 0] - 5)/2.533
为了更好的可读性,你可以使用 within
:
For even better readibility, you can use within
:
df <- within(df, est[b==0] <- (a[b==0]-5)/2.533)
结果,无论您选择哪种方法:
The results, regardless of which method you choose:
df
a b est
1 11.77000 2 0.000000
2 10.90000 3 0.000000
3 10.32000 2 0.000000
4 10.96000 0 2.352941
5 9.90600 0 1.936834
6 10.70000 0 2.250296
7 11.43000 1 0.000000
8 11.41000 2 0.000000
9 10.48512 4 0.000000
10 11.19000 0 2.443743
<小时>
正如其他人所指出的,您的示例中的替代解决方案是使用 ifelse
.
这篇关于有条件地替换 data.frame 中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!