Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find max value within a data frame interval

Tags:

dataframe

r

max

I have a dataframe that has x/y values every 5 seconds, with a depth value every second (time column). There is no depth where there is an x/y value.

x <- c("1430934", NA, NA, NA, NA, "1430939")
y <- c("4943206", NA, NA, NA, NA, "4943210")
time <- c(1:6)
depth <- c(NA, 10, 19, 84, 65, NA)
data <- data.frame(x, y, time, depth)
data 
     x       y      time depth
1 1430934 4943206    1    NA     
2    NA    NA        2    10     
3    NA    NA        3    19     
4    NA    NA        4    84     
5    NA    NA        5    65   
6 1430939 4943210    6    NA    

I would like to calculate the maximum depth between the x/y values that are not NA and add this to a new column in the row of the starting x/y values. So max depth of rows 2-5. An example of the output desired.

    x       y       time depth newvar
1 1430934 4943206    1    NA     84
2    NA    NA        2    10     NA
3    NA    NA        3    19     NA
4    NA    NA        4    84     NA
5    NA    NA        5    65     NA
6 1430939 4943210    6    NA     NA

This is to repeat whenever a new x/y value is present.

like image 955
Rachel Avatar asked Nov 19 '25 05:11

Rachel


1 Answers

You can use ave and cumsum with !is.na to get the groups for ave like:

data$newvar <- ave(data$depth, cumsum(!is.na(data$x)), FUN=
 function(x) if(all(is.na(x))) NA else {
  c(max(x, na.rm=TRUE), rep(NA, length(x)-1))})
data
#        x       y time depth newvar
#1 1430934 4943206    1    NA     84
#2    <NA>    <NA>    2    10     NA
#3    <NA>    <NA>    3    19     NA
#4    <NA>    <NA>    4    84     NA
#5    <NA>    <NA>    5    65     NA
#6 1430939 4943210    6    NA     NA
like image 130
GKi Avatar answered Nov 20 '25 19:11

GKi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!