我正在计算我们在trap 中捕捉小龙虾的距离的中值.我们有多年的trap 池塘,并知道我们设置的trap 数量和trap 的数量.

这是一个例子,我正在努力做什么.

# Create vectors for each column
Pond <- c("Aubrees", "Aubrees", "Kohls", "Kohls")
Year <- c(2019, 2020, 2019, 2020)
TrapNumbers <- list(c(1, 9, 2, 3, 6), c(3, 4, 9, 2, 5), c(3, 4, 9, 2), c(1, 9, 2, 3))
NumberofTraps <- c(10, 10, 12, 12)

# Initialize an empty vector to store all TrapCaught values
TrapCaught <- numeric()

# Iterate over the lists of TrapNumbers to combine them into one vector
for (trap_list in TrapNumbers) {
  TrapCaught <- c(TrapCaught, trap_list)
}

# Create the dataframe
DF <- data.frame(Pond = rep(Pond, sapply(TrapNumbers, length)),
                 Year = rep(Year, sapply(TrapNumbers, length)),
                 NumberofTraps = rep(NumberofTraps, sapply(TrapNumbers, length)),
                 TrapCaught = TrapCaught)

NumberofTraps列告诉我有多少个trap 设置在池塘中,在海岸线周围呈圆形排列.所以在一个有10个trap 的池塘里,1和9之间的距离是2而不是8.我也不想计算一个点和它自身之间的距离.当我为这些手动操作时,我找到了这些值的距离和中值的池塘和年份.

Aubrees 2019 中位距离:2,1,2,5,3,4,3,1,4,3 平均值=3

Aubrees 2020 中位距离:1,4,1,2,5,2,1,3,4,3 平均值=2.5

Kohls 2019 中间距离:1,6,1,5,2,5 中位数= 3.5

Kohls 2020 中间距离:4,1,2,5,6,1 中位数= 3

我有这个代码,我正在try ,但我没有得到期望值.

library(dplyr)

# Function to calculate pairwise distances between two vectors
pairwise_distances <- function(x) {
  n <- length(x)
  outer(x, x, "-") %% n
}

# Calculate the median distance for each group
median_distances <- DF %>%
  group_by(Pond, Year) %>%
  summarize(median_distance = median(pairwise_distances(TrapCaught)[upper.tri(pairwise_distances(TrapCaught))]))

# Print the result
print(median_distances)

The values output are: Pond: Aubrees, Aubrees, Kohls, Kohls Year: 2019, 2020, 2019, 2020 Code Median Output: 2, 2.5, 2.5. 2.5
Expected Median Outcome: 3, 2.5, 3.5, 3

推荐答案

你的pairwise_distances()函数不计算你所追求的成对距离.

PondYearTrapNumbersNumberofTraps开始:

library(dplyr)
library(purrr)

df <- tibble(Pond, Year, TrapNumbers, NumberofTraps)

pairwise_distance <- function(x, end) {
  a <- abs(outer(x, x, `-`))
  b <- abs(a - end)
  pmin(a, b)[upper.tri(a)]
}

output <- df |>
  mutate(distances = map2(TrapNumbers, NumberofTraps, pairwise_distance),
         median_distance = map_dbl(distances, median))

output$distances
# [[1]]
#  [1] 2 1 3 2 4 1 5 3 4 3
# 
# [[2]]
#  [1] 1 4 5 1 2 3 2 1 4 3
# 
# [[3]]
# [1] 1 6 5 1 2 5
# 
# [[4]]
# [1] 4 1 5 2 6 1

否则,如果您从DF开始,则可以修改上面的代码以使用以下命令创建相同的输出:

output <- DF |>
  summarize(distances = list(pairwise_distance(TrapCaught, unique(NumberofTraps))),
            median_distance = map_dbl(distances, median), .by = c(Pond, Year))

Output

  Pond     Year TrapNumbers NumberofTraps distances  median_distance
  <chr>   <dbl> <list>              <dbl> <list>               <dbl>
1 Aubrees  2019 <dbl [5]>              10 <dbl [10]>             3  
2 Aubrees  2020 <dbl [5]>              10 <dbl [10]>             2.5
3 Kohls    2019 <dbl [4]>              12 <dbl [6]>              3.5
4 Kohls    2020 <dbl [4]>              12 <dbl [6]>              3 

R相关问答推荐

通过绘图 Select 线串几何体并为其着色

在R中列表的结尾添加数字载体

如何使用shinyChatR包配置聊天机器人

如何 bootstrap glm回归、估计95%置信区间并绘制它?

过滤器数据.基于两列的帧行和R中的外部向量

使用geom_segment()对y轴排序

如何从像glm这样的模型中提取系数表的相关性?

迭代到DataFrame列并获得成对的值列表(col1->;col2、col2->;col3、col3->;col4等)的正确方法.

如何将R中数据帧中的任何Nas替换为最后4个值

从R中的对数正态分布生成随机数的正确方法

提高圣彼得堡模拟的速度

删除数据帧中特定行号之间的每第三行和第四行

网络抓取新闻标题和时间

如何在条形图中的x和填充变量中包含多个响应变量?

如何使投篮在R中保持一致

排序R矩阵的行和列

子样本间系数检验的比较

无法保存gglot的所有pdf元素

修复标签重叠和ggploy内的空间

如何根据每个子框架中分类因子的唯一计数来过滤子框架列表?