与 data.frame 中的交替行组进行比较时的计数频率

Counting frequency when comparing to alternating groups of rows in a data.frame

我有一个 table 我想要

实际上,我将一次使用四行作为参考集,其中将与以下四行中的每一行进行比较。

具体来说,给定 x 组中的一行,我想计算前一组(即 x-1 组)中有多少行的值小于或等于感兴趣行中的值.

我想对每一行都这样做。

因此,我想计算第二组四行中的每一行(比如 5 到 8),其值小于或等于第一组中的行数(比如第 1 至 4 行)。然后第 5 到 8 行成为接下来四行(9 到 12)的下一个参考组。等等...

Row Values
1   1.35
2   0.71
3   1.00
4   0.07
5   0.53
6   0.12
7   0.36
8   2.03
9   3.83
10  1.30
11  2.17
12  1.71
13  1.52
14  1.27
15  0.29
16  0.05
17  0.14

结果如下:

Row Values  Count
1   1.35    
2   0.71    
3   1.00    
4   0.07    
5   0.53    1
6   0.12    1
7   0.36    1
8   2.03    4
9   3.83    4
10  1.30    3
11  2.17    4
12  1.71    3
13  1.52    1
14  1.27    0
15  0.29    0
16  0.05    0
17  0.14    1

你可以试试(如果df是你的data.frame):

sdf<-split(df$Values,(df$Row-1)%/%4)
c(rep(NA,4),unlist(Map(f=function(x,y)
      findInterval(x,sort(y)),sdf[-1],sdf[-length(sdf)]),use.names=F))
#[1] NA NA NA NA  1  1  1  4  4  3  4  3  1  0  0  0  1

你可以试试这个:

dat<-data.frame(row=c(1:length(z)),Values=z,ceiling=c(rep(NA,length(z))),count=c(rep(NA,length(z))))
#where z is a vector of your values.

for(x in 1:dim(dat)[1]) {
    dat$ceiling[x]<-ceiling(x/4)
    dat$count[x]<-length(which(dat$Values[dat$ceiling == (dat$ceiling[x]-1)] <= dat$Values[x]))
}

ceiling 函数与 lapplyvapply 一起使用。

ceiling takes a single numeric argument x and returns a numeric vector containing the smallest integers not less than the corresponding elements of x

  • 要达到预期效果,请将 x 除以每组中所需的行数。

    ceiling(x/y) #where x = the row number and y = the number of rows per group
    

(假设 df 是您的 data.frame):

lapply:

z <- df$Values
Groups <- ceiling(seq(z)/4)
df$Count <- 
  unlist(lapply(seq(z), function(x) sum(z[x] >= z[Groups == Groups[x] - 1])))

vapply:

df$Count <- 
  vapply(seq(z), function(x) sum(z[x] >= z[Groups == Groups[x] - 1]), integer(1))

如果你想要一个命令:

df$Count <- 
  with(df,unlist(lapply(seq(Values), function(x) 
  sum(Values[x] >= Values[ceiling(seq(Values)/4) == ceiling(seq(Values)/4)[x] - 1]))))