R 连续变量上的聚合
假设我在R中有以下数据帧:R 连续变量上的聚合,r,dataframe,R,Dataframe,假设我在R中有以下数据帧: d <- read.table(text='Name Month Rate1 Rate2 Aira 1 12 23 Aira 2 18 73 Aira 3 19 45 Ben 1 53 19 Ben 2 22 87 Ben 3 19
d <- read.table(text='Name Month Rate1 Rate2
Aira 1 12 23
Aira 2 18 73
Aira 3 19 45
Ben 1 53 19
Ben 2 22 87
Ben 3 19 45
Cat 1 22 87
Cat 2 67 43
Cat 3 45 32', header=TRUE)
d找出哪些变量位于您所设计的分位数中,然后计算这些变量的平均值,如下所示:
q_use = list(c(0,0.25),c(0.26,0.5),c(0.51,0.75),c(0.76,1))
pos_use = lapply(1:length(q_use), function(x) quantile(d$Rate1,q_use[[x]]))
average_q = lapply(1:length(q_use), function(x)
mean(d[which(d$Rate1>=pos_use[[x]][1] & d$Rate1<=pos_use[[x]][2]),"Rate2"]))
q_use=list(c(0,0.25)、c(0.26,0.5)、c(0.51,0.75)、c(0.76,1))
pos_use=lappy(1:长度(q_use),函数(x)分位数(d$Rate1,q_use[[x]]))
平均值=lappy(1:长度(使用),函数(x)
平均值(d)[哪个(d$Rate1>=pos_使用[[x]][1]&d$Rate1如何找到哪些变量位于您所设计的分位数中,然后计算这些变量的平均值,如下所示:
q_use = list(c(0,0.25),c(0.26,0.5),c(0.51,0.75),c(0.76,1))
pos_use = lapply(1:length(q_use), function(x) quantile(d$Rate1,q_use[[x]]))
average_q = lapply(1:length(q_use), function(x)
mean(d[which(d$Rate1>=pos_use[[x]][1] & d$Rate1<=pos_use[[x]][2]),"Rate2"]))
q_use=list(c(0,0.25)、c(0.26,0.5)、c(0.51,0.75)、c(0.76,1))
pos_use=lappy(1:长度(q_use),函数(x)分位数(d$Rate1,q_use[[x]]))
平均值=lappy(1:长度(使用),函数(x)
平均值(d[which(d$Rate1>=pos_use[[x]][1]&d$Rate1您可以使用cut
查找四分位数并进行汇总,或者使用ave
创建列,具体取决于您希望输出的方式(感谢@akrun的注释)
您可以使用cut
查找四分位数并进行汇总,或者使用ave
创建列,具体取决于您希望输出的方式(感谢@akrun的注释)
似乎你的问题与这个问题非常相似:似乎你的问题与这个问题非常相似: