order_by 函数 R

order_by(order_by,call) 函数 更灵活的排序

This function makes it possible to control the ordering of window functions in R that don't have a specific ordering parameter. When translated to SQL it will modify the order clause of the OVER function.

第一个参数order_by是排序要依据的向量

第二个参数call是一个函数,其中第一个 参数是被处理的向量

例1

> cumsum(1:10)

[1] 1 3 6 10 15 21 28 36 45 55

> order_by(10:1,cumsum(1:10)) #按10:1向量顺序输出

[1] 55 54 52 49 45 40 34 27 19 10 

 

例2

df<-data.frame(year=2000:2005,value=(0.5)^2)

df

 

> scrambled<-df[sample(nrow(df)),] #默认的sample 是从向量中取出n次(n为向量元素个数)

> scrambled

 

 

 wrong<-mutate(scrambled,running=cumsum(value))

wrong

 

 arrange(wrong,year) #按year 排序

 

 

 

 

> right<-mutate(scrambled,running=order_by(year,cumsum(value)))

> right

 

 

 

 arrange(right,year)

 

posted on 2022-05-09 17:42  BioinformaticsMaster  阅读(184)  评论(0编辑  收藏  举报

导航