R语言 如何使用两个变量和一个查找表的唯一组合执行算术运算?

w8f9ii69  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(116)

我有一些按深度和基质类型分层的面积覆盖数据。我需要将面积转换为感兴趣物种的生物量。面积数据如下所示:

library(tidyverse)
dat <- tibble::tribble(
                  ~model_seg, ~start_depth_m, ~substrate, ~area_m2,
                           1,              0,     "hard",        0,
                           1,              0,     "sand",   482500,
                           1,              0,     "soft",        0,
                           1,              1,     "hard",        0,
                           1,              1,     "sand",   870000,
                           1,              1,     "soft",        0,
                           1,              2,     "hard",        0,
                           1,              2,     "sand",   700000,
                           1,              2,     "soft",     5000
                  )
dat

然后我对每个深度/基质配对的生物量进行了估计,如下所示:

sub.dat <- tibble::tribble(
                         ~rnd_depth, ~substrate,      ~biomass_gm2,
                                  0,     "hard",  11.6378195672474,
                                  0,     "sand", 0.480829306398132,
                                  0,     "soft",  2.16687001205995,
                                  1,     "hard",  23.6843572281917,
                                  1,     "sand", 0.674134215616002,
                                  1,     "soft",  3.35885439288727,
                                  2,     "hard",  41.0456045259747,
                                  2,     "sand",  1.75564256550627,
                                  2,     "soft",  5.34840185637371
                         )
sub.dat

我需要将每个基质/深度对的面积值转换为生物量值。实际数据包含基质/深度对,精确到20米,但对于示例数据,结果应类似于此数据集中的变量answer

ans <- tibble::tribble(
  ~model_seg, ~start_depth_m, ~substrate, ~area_m2,  ~answer,
           1,              0,     "hard",        0,        0,
           1,              0,     "sand",   482500, 232082.5,
           1,              0,     "soft",        0,        0,
           1,              1,     "hard",        0,        0,
           1,              1,     "sand",   870000,   586380,
           1,              1,     "soft",        0,        0,
           1,              2,     "hard",        0,        0,
           1,              2,     "sand",   700000,  1232000,
           1,              2,     "soft",     5000,    26750
  )
# EDIT: Perhaps it would be helpful to know that I created the answer column manually like this: 
a <- c(0,482500*0.481,0,
       0,870000*0.674,0,
       0,700000*1.76,5000*5.35)
# this vector was simply added to the 'ans' data object but it shows the calulation (area * biomass)

我试着使用我之前发布的关于查找表的问题:R- How do I use a lookup table containing threshold values that vary for different variables (columns) to replace values below those thresholds?但是我没有足够的能力使用替换功能来适应它。TIA

ogq8wdun

ogq8wdun1#

如果我没理解错的话...
您有两个 Dataframe :

  1. dat具有“深度”、“衬底类型”和“面积”的表。
  2. sub.dat具有“深度”、“基质类型”和“biomass_gm2”(特定物种从面积到生物量的转换因子)的表格。
    您希望同时使用“depth”和“substrate type”作为***复合键***,首先从sub.dat查找转换因子(biomass_gm2),然后取转换因子与area的乘积(biomass_gm2 * area)来计算最终生物量。
require(dplyr)

left_join(dat, sub.dat, by = c("start_depth_m" = "rnd_depth", "substrate" = "substrate")) %>%
mutate(answer = area_m2 * biomass_gm2) # editing the variable name

相关问题