简体   繁体   English

在R中,基于标志/因子值的分位数

[英]In R, Quantile based on flag/ factor value

I am trying to undertake some simple benchmarking in R. I have a dataframe with several numeric and a number of factors. 我正在尝试在R中进行一些简单的基准测试。我有一个包含多个数值和许多因素的数据框。

What I am trying to do is find the top decile and top quartile of a variable called ALoS based on the associated factor value and then attach these values back to the original dataframe 我正在尝试做的是根据关联的因子值找到称为ALoS的变量的最高十进制和最高四分位数,然后将这些值附加回原始数据帧

In excel this would be the equivalent of an array formula similar to: {=percentile(if(Factor_range = Factor, ALoS_range),k)} 在excel中,这等效于类似于以下数组公式: {=percentile(if(Factor_range = Factor, ALoS_range),k)}

Example

You seem to have two questions. 您似乎有两个问题。 As for the first, to compute the quantiles, since you haven't provided us with a dataset, I'll make up one. 至于第一个,为了计算分位数,因为您没有为我们提供数据集,所以我将组成一个。 See if the following answers the question. 查看以下内容是否回答了问题。

set.seed(954)
dat <- data.frame(A = sample(letters[1:3], 20, TRUE), X = rnorm(20))
dat
quantile(dat$X[dat$A == "a"], probs = c(0.75, 0.90))

As for the second question, to attach it back to the data frame, I really don't understand what you mean. 至于第二个问题,要将其附加到数据框中,我真的不明白您的意思。 Please give us an example of the wanted output. 请给我们一个想要的输出的例子。

This is a great time to use the ave function: 这是使用ave函数的好时机:

dat$top_q <- ave(dat$X, dat$A, FUN = function(x) quantile(x, .75))
dat$top_d <- ave(dat$X, dat$A, FUN = function(x) quantile(x, .9))

   A          X    top_q     top_d
1  a  1.7150650 1.346828 1.5677700
2  b  0.4609162 0.390532 0.4308438
3  a -1.2650612 1.346828 1.5677700
4  b -0.6868529 0.390532 0.4308438
5  b -0.4456620 0.390532 0.4308438
6  a  1.2240818 1.346828 1.5677700
7  b  0.3598138 0.390532 0.4308438
8  b  0.4007715 0.390532 0.4308438
9  b  0.1106827 0.390532 0.4308438
10 a -0.5558411 1.346828 1.5677700

data 数据

set.seed(123)
dat <- data.frame(A = sample(letters[1:2], 10, TRUE), X = rnorm(10))

   A          X
1  a  1.7150650
2  b  0.4609162
3  a -1.2650612
4  b -0.6868529
5  b -0.4456620
6  a  1.2240818
7  b  0.3598138
8  b  0.4007715
9  b  0.1106827
10 a -0.5558411

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

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