简体   繁体   English

R:将 which() 与 quantile() 的输出一起使用?

[英]R: Using which() with output of quantile()?

So, I'm trying to get the value in a column a from the data frame df, which is the 90th percentile of column b.所以,我试图从数据框 df 中获取 a 列中的值,这是 b 列的第 90 个百分位数。 I used the following code to get the value at quantile:我使用以下代码来获取分位数的值:

p = quantile(df$b, c(0.9))

After this, I wanted to use this to get the row number of the value, so that I could use that to get the corresponding value in column a:在此之后,我想用它来获取值的行号,以便我可以使用它来获取 a 列中的相应值:

which(df$b == p)

but for some reason it just gives an output of但由于某种原因,它只是给出了一个输出

integer(0)

I substituted the variable, p, with the actual value, which was 1.68, and it worked, and creating another variable with just 1.68 works too, but using it with the result of the quantile never gives the right value.我用实际值 1.68 替换了变量 p,它起作用了,并且创建另一个只有 1.68 的变量也起作用,但是将它与分位数的结果一起使用永远不会给出正确的值。

I've tried using as.numeric , using p[[1]] and as.double .我试过使用as.numeric ,使用p[[1]]as.double No change to the result whatsoever.结果没有任何改变。 Any help is appreciated in understanding why this happens and if there's another way to go about this.在理解为什么会发生这种情况以及是否有另一种方法来解决这个问题时,我们不胜感激。

Edit: to clarify, there are entries exactly at the 90th percentile, and using a hard coded value which was returned by the quantile function will return these.编辑:澄清一下,在第 90 个百分位数处有条目,并且使用分位数函数返回的硬编码值将返回这些。 The problem occurs in the which function when we use the output of quantile.当我们使用分位数的输出时,问题出现在 which 函数中。

'quantile' will give you the the 90% percentile of your data which may or may not be a value in your data therefore if you want the value closest to the 90% percentile you will need to match it a little differently: “分位数”将为您提供数据的 90% 百分位数,这可能是也可能不是您数据中的值,因此如果您想要最接近 90% 百分位数的值,您需要稍微不同地匹配它:

Your data:您的数据:

df <- data.frame(b = runif(50))

The 90th percentile第 90 个百分位

p = quantile(df$b, c(0.9))

The index the is closest to the 90th percentile:最接近第 90 个百分位数的指数:

index <- which(abs(df$b - p) == (min(abs(df$b - p), na.rm = TRUE)))
index

Get the value(s) from the dataframe using the index:使用索引从数据框中获取值:

df$b[index]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM