[英]Select multiple values from a column based on condition
I have this data frame called df
: 我有一个称为
df
数据框:
df <- structure(list(opty_id = c(123L, 147L, 165L, 198L, 203L, 209L, 219L, 271L, 284L),
amount_opty = c(855252.21, 590259.86, 742818.76, 742818.76, 211760, 149190.3, 118924.22, 414663.687, 403313.319 ),
percent = c(NA, 0.309841175, NA, 0, 0.714923732, 0.295474594, 0.202868953, NA, 0.027372467)),
.Names = c("opty_id", "amount_opty", "percent"),
class = "data.frame", row.names = c(NA, -9L))
The percent
column is the percentage increase compared to the previous row. percent
列是与上一行相比增加的百分比。
I need to find out the pair of rows where percent
is less than 10%. 我需要找出
percent
小于10%的那对行。
The desired output would be: id 8 and id 9 (basically its resulted id and -1) 所需的输出为: id 8和id 9 (基本上是其结果id和-1)
You can do this. 你可以这样做。
Divide previous diff
by current value
and find fraction (percentage): 将先前的
diff
除以当前value
然后求出分数(百分比):
c(df$diff[-1], NA) / df$value
# [1] 0.3333333 0.0000000 0.0400000 0.1666667 NA
Check if fraction difference is less than 0.1
(10%) 检查分数差异是否小于
0.1
(10%)
c(df$diff[-1], NA) / df$value < 0.1
# [1] FALSE TRUE TRUE FALSE NA
Find ID's in your data: 在您的数据中查找ID:
which(c(df$diff[-1], NA) / df$value < 0.1) + 1
# [1] 3 4
Data: 数据:
df <- structure(list(id = 1:5, value = c(60L, 40L, 50L, 48L, 40L),
diff = c(0L, 20L, 0L, 2L, 8L)), .Names = c("id", "value", "diff"), row.names = c(NA, -5L), class = "data.frame")
You can use which()
to find the elements in df$percent
that are smaller than 0.1 and also larger than 0: 您可以使用
which()
在df$percent
中查找小于0.1 且也大于0的元素:
main_id <- which(df$percent < 0.1 & df$percent > 0.0) # gives 9
target <- c(main_id - 1, main_id)
target
# [1] 8 9
If you don't want the row numbers, but you want the opty_id
instead, use: 如果您不想要行号,但是想要
opty_id
,请使用:
df$opty_id[target]
# [1] 271 284
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.