将 bin 限制从一个数据帧应用到 R 中的另一个数据帧

Apply bin limits from one data frame to another data frame in R

我正在尝试使用初始数据框创建具有等量观测值的箱子:

# create data frame
das <- data.frame(anim=1:15, count = 1,
              wt=c(181,179,180.5,201,201.5,245,246.4,
                   189.3,301,354,369,205,199,394,231.3))

## create 3 equal bins
das$bin <-cut2(das$wt, g=3)

## View number of values in each bin
das %>% 
   count(count, bin) %>% 
   kable(align ='c')

## verify that bins have equal number of observations
| count |    bin    | n |
|:-----:|:---------:|:-:|
|   1   | [179,201) | 5 |
|   1   | [201,246) | 5 |
|   1   | [246,394] | 5 |

然后我想使用上面定义的 bin 限制为第二个数据框创建 bin,但我不知道如何强制 bin 具有相同的大小:

das2 <- data.frame(anim=1:15, count = 1, wt=c(185,190,181,220,205,235,226,
                               189,304,303,179,205,199,394,231.3))

我的最终目标是通过回归 'das2' 中落入由 'das' 创建的箱子中的观察数量来创建 k 折交叉验证的回归——我正在尝试评估根据 GPS 遥测数据的 used/available 设计生成的资源选择函数的预测强度。

谢谢!

如果您想使用第一次切割的间隔来对第二个数据集进行分箱,您可以使用参数 onlycuts=TRUE return 它们并将它们提供给对 [=12= 的第二次调用]

library(Hmisc)
library(tidyverse)
library(knitr)

das <- data.frame(anim=1:15, count = 1,
                  wt=c(181,179,180.5,201,201.5,245,246.4,
                       189.3,301,354,369,205,199,394,231.3))

das2 <- data.frame(anim=1:15, count = 1, wt=c(185,190,181,220,205,235,226,
                                              189,304,303,179,205,199,394,231.3))

breaks <-cut2(das$wt, g=3, onlycuts = TRUE)

das2$bin<-cut2(das2$wt, cuts = breaks)

das2 %>% 
  count(count, bin) %>% 
  kable(align ='c')

| count |    bin    | n |
|:-----:|:---------:|:-:|
|   1   | [179,201) | 6 |
|   1   | [201,246) | 6 |
|   1   | [246,394] | 3 |