簡體   English   中英

R 根據參考值計算列

[英]R calculate column based on reference value

我有一個數據框,我希望在其上計算一個新列。 它將表示壓力變化的值轉換為相對於某物的值。 我有起始參考值 (=0.81),它將是新列中的第一個值。 然后,我需要通過添加代表壓力變化的相應列中的值來填充列的 rest。

數據框

這是數據框的片段(行數為 17500)。 唯一相關的列是最后一列。

structure(list(Rec = 1:10, DateTime = structure(c(1585468800, 
1585472400, 1585476000, 1585479600, 1585483200, 1585486800, 1585490400, 
1585494000, 1585497600, 1585501200), class = c("POSIXct", "POSIXt"
), tzone = "GMT"), TempHMB5 = c(6.78, 6.78, 6.78, 6.78, 6.78, 
6.77, 6.77, 6.77, 6.77, 6.76), PressHMB5 = c(1074.09, 1074.87, 
1074.8, 1075.02, 1074.95, 1074.95, 1074.59, 1074.45, 1074.45, 
1074.52), TempBaro = c(3.89, 5.1, 7.79, 8.89, 8.04, 8.01, 7.7, 
7.88, 7.25, 6.5), PressBaro = c(1053.98, 1054.5, 1054.53, 1054.71, 
1054.66, 1054.67, 1054.39, 1054.24, 1054.26, 1054.22), subPress = c(20.1099999999999, 
20.3699999999999, 20.27, 20.3099999999999, 20.29, 20.28, 20.1999999999998, 
20.21, 20.1900000000001, 20.3), subPressM = c(0.201099999999999, 
0.203699999999999, 0.2027, 0.203099999999999, 0.2029, 0.2028, 
0.201999999999998, 0.2021, 0.201900000000001, 0.203), subPressChange = c(0, 
0.00259999999999991, 0.00160000000000082, 0.00200000000000045, 
0.00180000000000063, 0.00170000000000073, 0.000899999999999179, 
0.00100000000000136, 0.000800000000001549, 0.00190000000000054
)), row.names = c(NA, 10L), class = "data.frame")

所需 Output

'relTo Dip' 中的最高值是已知參考值 = 0.81。 rest 的計算需要與第二列類似。 所以以前的值+新行中的變化。

在此處輸入圖像描述

您可以使用cumsum function。

# Concatenate your initial value (0.81) with the column on the left
  v <- c(0.81, 0.0026,0.0016,0.0020,0.0018, 0.0017, 0.0009, 0.0010, 0.0008, 0.0019)
# Calculate the "cummulative sum" of the elements and you get the column on the right
  cumsum(v)

累積金額會解決問題

library(dplyr)

df %>%
  mutate(
    relToDip = cumsum(subPressChange)+0.81
    )
#>    Rec            DateTime TempHMB5 PressHMB5 TempBaro PressBaro subPress
#> 1    1 2020-03-29 08:00:00     6.78   1074.09     3.89   1053.98    20.11
#> 2    2 2020-03-29 09:00:00     6.78   1074.87     5.10   1054.50    20.37
#> 3    3 2020-03-29 10:00:00     6.78   1074.80     7.79   1054.53    20.27
#> 4    4 2020-03-29 11:00:00     6.78   1075.02     8.89   1054.71    20.31
#> 5    5 2020-03-29 12:00:00     6.78   1074.95     8.04   1054.66    20.29
#> 6    6 2020-03-29 13:00:00     6.77   1074.95     8.01   1054.67    20.28
#> 7    7 2020-03-29 14:00:00     6.77   1074.59     7.70   1054.39    20.20
#> 8    8 2020-03-29 15:00:00     6.77   1074.45     7.88   1054.24    20.21
#> 9    9 2020-03-29 16:00:00     6.77   1074.45     7.25   1054.26    20.19
#> 10  10 2020-03-29 17:00:00     6.76   1074.52     6.50   1054.22    20.30
#>    subPressM subPressChange relToDip
#> 1     0.2011         0.0000   0.8100
#> 2     0.2037         0.0026   0.8126
#> 3     0.2027         0.0016   0.8142
#> 4     0.2031         0.0020   0.8162
#> 5     0.2029         0.0018   0.8180
#> 6     0.2028         0.0017   0.8197
#> 7     0.2020         0.0009   0.8206
#> 8     0.2021         0.0010   0.8216
#> 9     0.2019         0.0008   0.8224
#> 10    0.2030         0.0019   0.8243

代表 package (v2.0.1) 於 2022 年 7 月 29 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM