Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(R) :通过唯一行值统一计算分位数_R_Aggregate_Quantile_Unification - Fatal编程技术网

(R) :通过唯一行值统一计算分位数

(R) :通过唯一行值统一计算分位数,r,aggregate,quantile,unification,R,Aggregate,Quantile,Unification,我有这样一个df: > df<-data.frame(Client.code = c(100451,100451,100523,100523,100523,100525),dayref = c(24,30,15,13,17,5)) > df Client.code dayref 1 100451 24 2 100451 30 3 100523 15 4 100523 13 5 10052

我有这样一个df:

> df<-data.frame(Client.code = 
c(100451,100451,100523,100523,100523,100525),dayref = c(24,30,15,13,17,5))
> df
    Client.code dayref
1      100451     24
2      100451     30
3      100523     15
4      100523     13
5      100523     17
6      100525      5
   Client.Code    Days
1  100451          16
1  100523          16
1  100460          35

因为我有足够的数据来计算一个合理的分位数。计算。我想知道如何建立一个循环,根据第一个df,分配这df2天中的每一行一个分位数。

我们可以使用
数据。表

library(data.table)
setDT(df)[, .(Quantile = quantile(dayref)), Client.code]

或使用
tidyverse

library(dplyr)
library(tidyr)
df %>% 
   group_by(Client.code) %>%
   summarise(Quantile = list(quantile(dayref))) %>%
   unnest
您可以通过添加特定的百分位数向量来指定它们

tapply(df$dayref, df$Client.code, quantile, 1:19/20)
你可能需要这样表述

tapply(df$dayref, df$Client.code, quantile, probs = 1:19/20)

如果可能有NAs,您可以添加na.rm=TRUE作为另一个参数

Try
library(data.table);setDT(df)[,分位数(dayref),(Client.code)]
这是一个很好的解决方案。如果有兴趣的话,我想问两个相关的问题:1)如何在xlsx中编写这个数组。2) 如果一个循环可以表示与给定数字向量相关的分位数,那么写的次数就越多。但不是很有用,可能是因为我的问题不够好。在给定的向量下,可以根据过去的分布指定一个分位数。谢谢@lvaroRodríguez如果您有不同的情况,您可以更新您的问题或创建新问题吗@lvaroRodríguez您可以做类似于
setDT(df)[,as.list(quantile(dayref)),Client.code][df2,on=(Client.code=Client.code)]
tapply(df$dayref, df$Client.code, quantile, probs = 1:19/20)