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

How to calculate a variable that select the maximum range value out of several consecutive columns

$
0
0

I have a dataframe df that summarizes hourly water temperatures at different depths (5 meters T5, 15 meters T15, 25 meters T25 and 35 meters T35) over several months. As an example:

df<- data.frame(DateTime=c("2018-08-09 08:00:00","2018-08-09 09:00:00","2018-08-09 10:00:00","2018-08-09 11:00:00","2018-08-09 12:00:00","2018-08-09 13:00:00"),
                T5=c(14.5,18.4,21.3,27.8,16.5,21.5),
                T15=c(13.8,16.3,16.2,17.8,19.3,20.1),
                T25=c(16.0,17.2,18.3,15.9,21.4,17.3),
                T35=c(16.1,15.7,16.2,15.6,17.0,16.3))

df$DateTime<- as.POSIXct(df$DateTime, formtat="%Y-%m-%d %H:%M:%S",tz="UTC")

df

             DateTime   T5  T15  T25  T35
1 2018-08-09 08:00:00 14.5 13.8 16.0 16.1
2 2018-08-09 09:00:00 18.4 16.3 17.2 15.7
3 2018-08-09 10:00:00 21.3 16.2 18.3 16.2
4 2018-08-09 11:00:00 27.8 17.8 15.9 15.6
5 2018-08-09 12:00:00 16.5 19.3 21.4 17.0
6 2018-08-09 13:00:00 21.5 20.1 17.3 16.3

What I want is to calculate some variables related to the difference in temperatures between depths. I would like to calculate the variable "Maximum Water Temperature Chance in the Column" (MWTCC) that reflects the maximum water temperature change betweem CONSECUTIVE columns for the whole water column. Later on, I would like to calculate also the variable "Water Temperture Change between 5 and 15 meters" (WTC10), "Water Temperature Change between 15 and 25 meters" (WTC20) and "Water Temperature Change between 25 and 35 meters" (WTC30). I would expect the next result from the mentioned example:

> df
             DateTime   T5  T15  T25  T35 MWTCC WTC10 WTC20 WTC30
1 2018-08-09 08:00:00 14.5 13.8 16.0 16.1   2.2   0.7   2.2   0.1
2 2018-08-09 09:00:00 18.4 16.3 17.2 15.7   2.1   2.1   0.9   1.5
3 2018-08-09 10:00:00 21.3 16.2 18.3 16.2   5.1   5.1   2.1   2.1
4 2018-08-09 11:00:00 27.8 17.8 15.9 15.6  10.0  10.0   1.9   0.3
5 2018-08-09 12:00:00 16.5 19.3 21.4 17.0   4.4   2.8   2.1   4.4
6 2018-08-09 13:00:00 21.5 20.1 17.3 16.3   2.8   1.4   2.8   1.0

Is there any easy and fast way to calculate all? I am especially interested in using the package data.table, although any way could be fine.

Thanks in advance


Viewing all articles
Browse latest Browse all 201894

Trending Articles



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