W (blue line below) in my data.frame represents where the water level in the braided river intersects the elevation profile.
For each survey date, I need to calculate the width of the braided river (sum of the widths of the blue lines below).
For example, on 2000-07-09
> head(df[16:39,], 24)
Date width code_w code_w1 width1
16 2000-07-09 59.60 <NA> <NA> NA
17 2000-07-09 60.60 <NA> <NA> NA
18 2000-07-09 60.72 <NA> <NA> NA
19 2000-07-09 60.76 W W 60.76
20 2000-07-09 61.24 <NA> W NA
21 2000-07-09 62.37 <NA> W NA
22 2000-07-09 63.41 <NA> W NA
23 2000-07-09 64.35 W W 64.35
24 2000-07-09 64.63 <NA> <NA> NA
25 2000-07-09 65.95 <NA> <NA> NA
26 2000-07-09 68.23 <NA> <NA> NA
27 2000-07-09 70.03 <NA> <NA> NA
28 2000-07-09 72.03 <NA> <NA> NA
29 2000-07-09 74.59 <NA> <NA> NA
30 2000-07-09 76.16 <NA> <NA> NA
31 2000-07-09 78.62 <NA> <NA> NA
32 2000-07-09 79.43 W W 79.43
33 2000-07-09 80.59 <NA> W NA
34 2000-07-09 81.67 <NA> W NA
35 2000-07-09 82.51 <NA> W NA
36 2000-07-09 83.56 W W 83.56
37 2000-07-09 83.87 <NA> <NA> NA
38 2000-07-09 85.24 <NA> <NA> NA
39 2000-07-09 86.28 <NA> <NA> NA
The total width
will be
(64.35-60.76) (83.56-79.43) = 7.72
I tried many things but my trials were not successful. Your help will be appreciated.
DATA
> dput(df)
structure(list(Date = structure(c(11147, 11147, 11147, 11147,
11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147,
11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147,
11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147,
11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147,
11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147,
11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147,
11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147,
11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147, 11147,
11147, 13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285,
13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285,
13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285,
13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285,
13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285,
13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285,
13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285, 13285,
13285, 13285, 13285, 13285, 15354, 15354, 15354, 15354, 15354,
15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354,
15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354,
15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354,
15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354,
15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354,
15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354,
15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354,
15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354,
15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354,
15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354, 15354,
15354, 15354, 15354, 15354, 16138, 16138, 16138, 16138, 16138,
16138, 16138, 16138, 16138, 16138, 16138, 16138, 16138, 16138,
16138, 16138, 16138, 16138, 16138, 16138, 16138, 16138, 16138,
16138, 16138, 16138, 16138, 16138, 16138, 16138, 16138, 16138,
16138, 16138, 16138, 16138, 16138, 16138, 16138, 16138, 16138,
16138, 16138, 16138, 16138, 16138), class = "Date"), width = c(0,
1.9300003, 4.97999954, 18.869999883, 31.029999492, 40.12999916,
45.09000015, 46.39999962, 46.40999985, 48.40999985, 50.02999878,
51.34000015, 52.63999939, 55.4300003, 57.27999878, 59.60000038,
60.59999847, 60.72000122, 60.75999832, 61.23999786, 62.36999893,
63.40999985, 64.34999847, 64.63000107, 65.95000076, 68.22999954,
70.02999878, 72.02999878, 74.59000015, 76.15999985, 78.61999893,
79.4300003, 80.59000015, 81.66999817, 82.50999832, 83.56000137,
83.86999893, 85.23999786, 86.27999878, 88.86000061, 93.31000137,
97.8900032, 103.2100029, 108.99999618, 112.52000046, 113.28000259,
113.81000137, 114.72999954, 117.52000046, 121.74000168, 126.15999985,
130.81000134, 136.64000324, 144.45000074, 152.02000044, 157.53000264,
161.81999584, 165.31999584, 168.09999464, 171.34000014, 177.50000384,
183.12999344, 185.00999834, 188.28999714, 191.36000444, 192.20000074,
194.97000504, 195.45999524, 195.97000504, 196.23999404, 197.09000014,
197.50999834, 197.78000264, 199.12999344, 199.53999714, 199.65999224,
200.31000134, 0, 0, 1.80000115, 2.03000069, 2.96999931, 3.48999977,
6.76000023, 8.76000023, 11.62000084, 16.96000004, 23.070000173,
29.640000225, 37.080000402, 41.27000046, 44.98999977, 46.85000038,
47.20000076, 48.02000046, 49.88000107, 52.87000084, 55.18000031,
58.87000084, 61.96000099, 61.96000099, 62.50000191, 63.51000023,
64.71000099, 65.41000176, 66.51000023, 68.28000069, 70.71000099,
72.50000191, 73.48999977, 75.46000099, 79.42000008, 84.23999977,
91.14999962, 97.09000206, 101.56000328, 108.02999687, 112.89999962,
112.89999962, 115.14999962, 120.09999657, 127.14999962, 135.35999873,
145.63999753, 153.87000083, 160.62999533, 168.20999713, 172.03999903,
177.73999593, 181.20999713, 183.28000453, 184.50000573, 185.70999713,
190.58000753, 194.70000263, 196.54999353, 197.56000333, 198.67999843,
199.40999413, 201.15999413, 201.56999783, 201.59000203, 201.59000203,
0, 0, 1.4300003, 2.97999954, 3.10000038, 3.22999954, 4.65999984,
7.56999969, 8.30999946, 8.63999939, 9.20999908, 9.72999954, 10.39999961,
11.11000061, 11.23999977, 11.62999916, 12.62999916, 14.15999984,
17.14000034, 20.05000019, 29.309999939, 38.79999971, 45.30000019,
46.72000026, 48.28000068, 49.10000038, 50.57999992, 51.59000015,
52.92000007, 54.45000076, 56.81999969, 60.81999969, 62.37000084,
63.46000099, 63.46000099, 64.41000175, 64.98999977, 66.83999824,
67.289999, 68.789999, 70.41000175, 71.26000023, 74.23999977,
76.32999992, 77.5700016, 79.46999931, 80.30999946, 81.32999992,
82.71999931, 84.24999809, 86.21000099, 86.42999839, 91.19999885,
96.13999748, 99.14999961, 102.34000206, 104.64999961, 105.8599987,
106.67999839, 107.38000297, 107.38999748, 108.039999, 109.67999839,
113.09999656, 113.09999656, 115.02000236, 119.45999717, 126.64999961,
133.31999782, 143.42999842, 151.77000232, 159.77999682, 166.43000602,
174.57000542, 179.04000662, 181.02000232, 183.35999872, 185.33999442,
186.93000602, 189.82000542, 191.77999682, 193.25999262, 195.10999872,
197.68000602, 198.13000302, 198.74999812, 198.91999622, 199.38000302,
200.02000232, 200.30000112, 200.69999502, 200.69999502, 200.94999502,
200.94999502, 201.29000662, 201.91000172, 202.38999752, 202.63999752,
202.85999872, 0, 61.66999817, 61.6800003, 61.97000122, 62.4300003,
62.72999954, 62.79000091, 62.95000076, 63.41999817, 63.97999954,
64.43999862, 64.81999969, 65.36000061, 65.95000076, 66.59999847,
67.45000076, 67.66999817, 67.95000076, 69.23999786, 70.13000107,
71.00999832, 72.95999908, 74.54999923, 76.11000061, 76.97000122,
77.86999893, 79.54999923, 80.29999923, 81.57999801, 82.61000061,
84.6800003, 87.68999862, 90.29999923, 92.09000015, 93.92000198,
96.47999954, 98.06000137, 99.77000046, 101.38000107, 102.72999954,
104.09000015, 105.6400032, 107.77000046, 109.39999771, 109.7899971,
110.03000259), code_w = c(NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, "W", NA, NA, NA, "W", NA,
NA, NA, NA, NA, NA, NA, NA, "W", NA, NA, NA, "W", NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
"W", NA, NA, NA, NA, NA, "W", NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, "W", NA, NA, NA, NA, "W", NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "W",
NA, NA, NA, NA, NA, "W", NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
"W", NA, NA, NA, NA, NA, NA, NA, "W", NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, "W", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
"W", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
code_w1 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, "W", "W", "W", "W", "W", NA, NA,
NA, NA, NA, NA, NA, NA, "W", "W", "W", "W", "W", NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, "W", "W", "W", "W", "W", "W", "W", NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "W", "W", "W",
"W", "W", "W", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "W", "W",
"W", "W", "W", "W", "W", NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, "W", "W", "W", "W", "W", "W", "W", "W", "W", NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, "W", "W", "W", "W", "W",
"W", "W", "W", "W", "W", "W", "W", NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA), width1 = c(NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 60.75999832,
NA, NA, NA, 64.34999847, NA, NA, NA, NA, NA, NA, NA, NA,
79.4300003, NA, NA, NA, 83.56000137, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 64.71000099, NA, NA, NA, NA, NA, 73.48999977, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 8.63999939, NA,
NA, NA, NA, 11.23999977, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
96.13999748, NA, NA, NA, NA, NA, 107.38000297, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 198.74999812, NA, NA, NA, NA,
NA, NA, NA, 200.94999502, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 62.72999954, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 67.66999817, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA)), row.names = c(NA, -288L), class = c("tbl_df",
"tbl", "data.frame"))
CodePudding user response:
library(tidyverse)
df %>%
drop_na(width1) %>%
group_by(Date) %>%
summarise(total_width = sum(width1 * c(-1, 1)), .groups = 'drop')
# A tibble: 4 x 2
Date total_width
<date> <dbl>
1 2000-07-09 7.72
2 2006-05-17 8.78
3 2012-01-15 16.0
4 2014-03-09 4.94