Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

How to calculate maximum diferences among different number of columns for different variables

$
0
0

I have a dataframe (df) with average Current Intensities for two different sites (A and B) and for different depths (5 meters C.I.5m, 12 meters C.I.12m, 20 meters C.I.20m, 28 meters C.I.28m and 35 meters C.I.35m) over time. Here I show an example:

df<- data.frame(Datetime=c("2018-08-06 00:00:00","2018-08-06 00:00:00","2018-08-06 03:00:00","2018-08-06 03:00:00","2018-08-06 06:00:00","2018-08-06 06:00:00"),
                Site=c("A","B","A","B","A","B"),
                C.I.5m=c(0.1,0.3,0.8,0.2,0.4,0.2),
                C.I.12m=c(0.2,0.1,0.6,0.3,0.2,0.4),
                C.I.20m=c(0.1,0.3,0.7,0.4,0.4,0.2),
                C.I.28m=c(0.2,0.3,0.4,0.1,0.1,0.2),
                C.I.35m=c(0.3,0.5,0.2,0.3,0.4,0.1))
df

             Datetime Site C.I.5m C.I.12m C.I.20m C.I.28m C.I.35m
1 2018-08-06 00:00:00    A    0.1     0.2     0.1     0.2     0.3
2 2018-08-06 00:00:00    B    0.3     0.1     0.3     0.3     0.5
3 2018-08-06 03:00:00    A    0.8     0.6     0.7     0.4     0.2
4 2018-08-06 03:00:00    B    0.2     0.3     0.4     0.1     0.3
5 2018-08-06 06:00:00    A    0.4     0.2     0.4     0.1     0.4
6 2018-08-06 06:00:00    B    0.2     0.4     0.2     0.2     0.1

I want to calculate how much differ the current intensity among depths (that is, among columns in my dataframe) with different variables. The first variable I call it MCICC(Maximum Current Intensity Change in Column) and is the maximum difference among the values from the different columns related to the current intensity (C.I.5m,C.I.12m,C.I.20m,C.I.28m and C.I.35m). Then, another variable called MCIC10m that summarizes the difference between C.I.5m and C.I.12m. Then another one called MCIC20m that summarizes the difference between C.I.12m, C.I.20m and C.I.28m. Finally, a variable called MCIC30m that summarizes the difference between C.I.28m and C.I.35m.

I would expect this:

> df
             Datetime Site C.I.5m C.I.12m C.I.20m C.I.28m C.I.35m MWCICC MWCIC10 MWCIC20 MWCIC30
1 2018-08-06 00:00:00    A    0.1     0.2     0.1     0.2     0.3    0.2     0.1     0.1     0.1
2 2018-08-06 00:00:00    B    0.3     0.1     0.3     0.3     0.5    0.4     0.2     0.2     0.2
3 2018-08-06 03:00:00    A    0.8     0.6     0.7     0.4     0.2    0.6     0.2     0.3     0.2
4 2018-08-06 03:00:00    B    0.2     0.3     0.4     0.1     0.3    0.3     0.1     0.3     0.2
5 2018-08-06 06:00:00    A    0.4     0.2     0.4     0.1     0.4    0.3     0.2     0.3     0.3
6 2018-08-06 06:00:00    B    0.2     0.4     0.2     0.2     0.1    0.3     0.2     0.2     0.1

The tricky point is that each new variable is calculated from a different number of primary columns. MCICC takes the 5 depths into account (five columns), MCIC10 takes 5 and 12 meters depth into account (two columns), MCIC20 takes 12, 20 and 28 meters depth into account (two columns) and MCIC30 takes 28 and 35 meters depth into account (three columns).

Does anyone know how to calculate all at once?


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>