如何使用r中“下一个”组的第一个值?

乔纳森

我试图获取r中下一组的第一个值以估计比率。我已经基于df中的type列创建了一个组。然后使用组内的样本位置估计一些影响因素。最后,我试图估计一个这样的比率:RRF=response/(F1*first(response)+(F2*??????))哪里F1*first(response)是组中的cal,但我不知道如何调用下一组的第一个值来完成比率。有人可以帮忙吗?这是我的代码和数据:

library(dplyr)
 conc_zero_test <- zero_test %>% 
  gather(gas, response, -time,-type)%>%
  group_by(group = cumsum(type == "current_std"),gas)%>%
  mutate(X1= row_number()-1, #estimates the position of the sample within the group -1 removes std
         F1=1-(X1/n()), #relative factor influence of the cal in the current group
         F2=1-F1,       #relative factor influence of the cal in the next group
         RRF=response/(F1*first(response)+(F2*????))

structure(list(time = structure(c(1564468200, 1564475400, 1564484400, 
1564486200, 1564493400, 1564497000, 1564498800, 1564506000, 1564509600, 
1564511400, 1564518600, 1564522200, 1564524000, 1564527600, 1564531200
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), type = c("current_std", 
"n2", "n2", "current_std", "n2", "-", "current_std", "-", "n2", 
"current_std", "n2", "-", "current_std", "-", "-"), ben = c(2293951.5, 
12703.1, 6392.7, 1762512.6, 10748.4, 25468.3, 1597679, 24400.4, 
6019.4, 1510760.2, 10329.1, 29292.6, 1495942.8, 61227.5, 25379.5
), xyl = c(210975.6, 4482, 2910.8, 127612.4, 3792.6, 10295.7, 
113439.1, 10628.8, 2064.3, 107134.3, 3764.1, 10380.6, 107353.6, 
23639.1, 10317.4), cym = c(546894.5, 12202.6, 8400.8, 302091.6, 
11072.2, 16349.2, 291637.5, 18891.8, 6500.7, 276997.5, 10821.2, 
18672, 274149.4, 61379.2, 19254.7), isop = c(397288.2, 0, 0, 
239779.9, 0, 1364.8, 199081.5, 1511.2, 0, 179364, 0, 1318.4, 
174450.7, 7137.5, 9567), macr = c(221195.8, 0, 0, 138806.3, 0, 
0, 116644, 0, 0, 108893.3, 0, 0, 105689, 4325.4, 0), pin = c(50795.3, 
0, 0, 28436, 0, 1020.1, 26482.9, 925.2, 0, 27394.1, 0, 989.7, 
24344.6, 1414.7, 736.3), tmb = c(9314.5, 0, 0, 5798, 0, 0, 5136.4, 
2252.5, 0, 4542.9, 0, 0, 4398.4, 3794.4, 2186.3), tol = c(880567.1, 
7430.6, 4225.5, 569616.2, 6091.8, 65642.6, 495780.5, 52129.9, 
3226, 456079.6, 5874, 34725.9, 453944.8, 56594.4, 66148.1), mvk = c(169036.8, 
0, 0, 108738, 0, 0, 56712.5, 0, 0, 79148.9, 0, 0, 64065, 0, 0
), euc = c(12815.2, 0, 0, 8012.6, 0, 0, 5411.8, 0, 0, 5839.9, 
0, 491.7, 5450.7, 1990.8, 500.7)), class = c("spec_tbl_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -15L), spec = structure(list(
    cols = list(time = structure(list(format = ""), class = c("collector_datetime", 
    "collector")), type = structure(list(), class = c("collector_character", 
    "collector")), ben = structure(list(), class = c("collector_double", 
    "collector")), xyl = structure(list(), class = c("collector_double", 
    "collector")), cym = structure(list(), class = c("collector_double", 
    "collector")), isop = structure(list(), class = c("collector_double", 
    "collector")), macr = structure(list(), class = c("collector_double", 
    "collector")), pin = structure(list(), class = c("collector_double", 
    "collector")), tmb = structure(list(), class = c("collector_double", 
    "collector")), tol = structure(list(), class = c("collector_double", 
    "collector")), mvk = structure(list(), class = c("collector_double", 
    "collector")), euc = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 2), class = "col_spec"))

预期输出示例

time                type        gas   response group    X1    F1    F2     RRF
  <dttm>              <chr>       <chr>    <dbl> <int> <dbl> <dbl> <dbl>   <dbl>
1 2019-07-30 06:30:00 current_std ben   2293952.     1     0 1     0     1      
2 2019-07-30 08:30:00 n2          ben     12703.     1     1 0.667 0.333 0.006005
3 2019-07-30 11:00:00 n2          ben      6393.     1     2 0.333 0.667 0.003962
     
蒂姆·范范

我将使用自联接来获取response下一组的第一个:

library(tidyverse)

# the OPs example data (long!)
zero_test <-
  structure(
    list(
      time = structure(
        c(
          1564468200,
          1564475400,
          1564484400,
          1564486200,
          1564493400,
          1564497000,
          1564498800,
          1564506000,
          1564509600,
          1564511400,
          1564518600,
          1564522200,
          1564524000,
          1564527600,
          1564531200
        ),
        class = c("POSIXct", "POSIXt"),
        tzone = "UTC"
      ),
      type = c(
        "current_std",
        "n2",
        "n2",
        "current_std",
        "n2",
        "-",
        "current_std",
        "-",
        "n2",
        "current_std",
        "n2",
        "-",
        "current_std",
        "-",
        "-"
      ),
      ben = c(
        2293951.5,
        12703.1,
        6392.7,
        1762512.6,
        10748.4,
        25468.3,
        1597679,
        24400.4,
        6019.4,
        1510760.2,
        10329.1,
        29292.6,
        1495942.8,
        61227.5,
        25379.5
      ),
      xyl = c(
        210975.6,
        4482,
        2910.8,
        127612.4,
        3792.6,
        10295.7,
        113439.1,
        10628.8,
        2064.3,
        107134.3,
        3764.1,
        10380.6,
        107353.6,
        23639.1,
        10317.4
      ),
      cym = c(
        546894.5,
        12202.6,
        8400.8,
        302091.6,
        11072.2,
        16349.2,
        291637.5,
        18891.8,
        6500.7,
        276997.5,
        10821.2,
        18672,
        274149.4,
        61379.2,
        19254.7
      ),
      isop = c(
        397288.2,
        0,
        0,
        239779.9,
        0,
        1364.8,
        199081.5,
        1511.2,
        0,
        179364,
        0,
        1318.4,
        174450.7,
        7137.5,
        9567
      ),
      macr = c(
        221195.8,
        0,
        0,
        138806.3,
        0,
        0,
        116644,
        0,
        0,
        108893.3,
        0,
        0,
        105689,
        4325.4,
        0
      ),
      pin = c(
        50795.3,
        0,
        0,
        28436,
        0,
        1020.1,
        26482.9,
        925.2,
        0,
        27394.1,
        0,
        989.7,
        24344.6,
        1414.7,
        736.3
      ),
      tmb = c(
        9314.5,
        0,
        0,
        5798,
        0,
        0,
        5136.4,
        2252.5,
        0,
        4542.9,
        0,
        0,
        4398.4,
        3794.4,
        2186.3
      ),
      tol = c(
        880567.1,
        7430.6,
        4225.5,
        569616.2,
        6091.8,
        65642.6,
        495780.5,
        52129.9,
        3226,
        456079.6,
        5874,
        34725.9,
        453944.8,
        56594.4,
        66148.1
      ),
      mvk = c(169036.8,
              0, 0, 108738, 0, 0, 56712.5, 0, 0, 79148.9, 0, 0, 64065, 0, 0),
      euc = c(
        12815.2,
        0,
        0,
        8012.6,
        0,
        0,
        5411.8,
        0,
        0,
        5839.9,
        0,
        491.7,
        5450.7,
        1990.8,
        500.7
      )
    ),
    class = c("spec_tbl_df", "tbl_df",
              "tbl", "data.frame"),
    row.names = c(NA,-15L),
    spec = structure(list(
      cols = list(
        time = structure(list(format = ""), class = c("collector_datetime",
                                                      "collector")),
        type = structure(list(), class = c("collector_character",
                                           "collector")),
        ben = structure(list(), class = c("collector_double",
                                          "collector")),
        xyl = structure(list(), class = c("collector_double",
                                          "collector")),
        cym = structure(list(), class = c("collector_double",
                                          "collector")),
        isop = structure(list(), class = c("collector_double",
                                           "collector")),
        macr = structure(list(), class = c("collector_double",
                                           "collector")),
        pin = structure(list(), class = c("collector_double",
                                          "collector")),
        tmb = structure(list(), class = c("collector_double",
                                          "collector")),
        tol = structure(list(), class = c("collector_double",
                                          "collector")),
        mvk = structure(list(), class = c("collector_double",
                                          "collector")),
        euc = structure(list(), class = c("collector_double",
                                          "collector"))
      ),
      default = structure(list(), class = c("collector_guess",
                                            "collector")),
      skip = 2
    ), class = "col_spec")
  )

temp1 <- zero_test %>% 
  gather(gas, response, -time,-type) %>%
  group_by(group = cumsum(type == "current_std"), gas) %>%
  mutate(X1= row_number()-1, #estimates the position of the sample within the group -1 removes std
         F1=1-(X1/n()), #relative factor influence of the cal in the current group
         F2=1-F1,
         first_response = first(response)) %>% 
  ungroup

conc_zero_test <- temp1 %>% 
  left_join(y = {temp1 %>%
                  mutate(group = group - 1) %>% 
                  select(gas, group, first_response_next = first_response) %>% 
                  distinct},
            by = c("gas", "group")) %>% 
  mutate(RRF = response / ((F1 * first_response) + (F2 * first_response_next)))

conc_zero_test
#> # A tibble: 150 x 11
#>    time                type  gas   response group    X1    F1    F2
#>    <dttm>              <chr> <chr>    <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 2019-07-30 06:30:00 curr… ben   2293952.     1     0 1     0    
#>  2 2019-07-30 08:30:00 n2    ben     12703.     1     1 0.667 0.333
#>  3 2019-07-30 11:00:00 n2    ben      6393.     1     2 0.333 0.667
#>  4 2019-07-30 11:30:00 curr… ben   1762513.     2     0 1     0    
#>  5 2019-07-30 13:30:00 n2    ben     10748.     2     1 0.667 0.333
#>  6 2019-07-30 14:30:00 -     ben     25468.     2     2 0.333 0.667
#>  7 2019-07-30 15:00:00 curr… ben   1597679      3     0 1     0    
#>  8 2019-07-30 17:00:00 -     ben     24400.     3     1 0.667 0.333
#>  9 2019-07-30 18:00:00 n2    ben      6019.     3     2 0.333 0.667
#> 10 2019-07-30 18:30:00 curr… ben   1510760.     4     0 1     0    
#> # … with 140 more rows, and 3 more variables: first_response <dbl>,
#> #   first_response_next <dbl>, RRF <dbl>

reprex软件包(v0.3.0)创建于2020-08-16

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Java获取下一个Enum值或从第一个开始

熊猫从组中获取列的第一个和最后一个值

使用RLE从组中获取第一个和最后一个值

使第一个div不推动下一个

使用同一组中满足条件的下一个第一行设置列值

选择下一个第一个div

如何阻止下一个按钮显示第一个div?

删除第一个项目时实例化下一个项目

删除组中的第一个和最后一个观测值

如何删除第一个元素之后的下一个元素?

当第一个输入值的长度为3时,如何关注下一个输入?

将下一个值移动到第一个空行熊猫

如果第一个复选框是在jQuery中,如何获取下一个复选框的值?

如何在下一个内容淡入之前使第一个内容完全淡出?

Pig如何传递第一个作业及其下一个作业中的数据

如何从Python中的上一个值中减去下一个值?

如何只影响第一个下一个特定的div

MongoDB查找下一个文档或转到第一个

如何使div内的2个跨度在一行中,以使下一个div的内容不会到达第一个div的位置?

将第一个迭代输入的值更改为下一个迭代输入值

如何从 twbs 分页插件中删除“第一个”、“上一个”、“下一个”和“最后一个”按钮

重用行中的最后一个值并在下一个 - SQL 服务器上创建第一个值

将每列中的第一个值乘以 MATLAB 中的下一个值

查找数组中值的第一个/下一个位置

如何确定 First 的使用范围以仅获取组中的第一个值?

删除实体的第一个请求比下一个更长

如何将分区中的最后一个条目与下一个中的第一个条目合并?

字典列表将第一个字典的第一个值与下一个字典的第一个值进行比较

从 1:nth 值创建数据帧的迭代,然后删除第一个值并在行中添加下一个值