finc
finc

Reputation: 49

Using R to extract multiple rows from data frame based on change in values

I have a data frame of temperature change over time. I would like to identify the first instance of when the temperature increases and decreases by 3, and extract those rows to place into a new data frame with time and a new column of 1's and 0's for On and Off. Please see below for example:

Original data frame:

Date        Time        Temp
2020-01-01  18:00:00    2
2020-01-01  18:00:10    2
2020-01-01  18:00:20    2
2020-01-01  18:00:30    2
2020-01-01  18:00:40    2
2020-01-01  18:00:50    2
2020-01-01  18:01:00    6
2020-01-01  18:01:10    10
2020-01-01  18:01:20    12
2020-01-01  18:01:30    12
2020-01-01  18:01:40    12
2020-01-01  18:01:50    12
2020-01-01  18:02:00    12
2020-01-01  18:02:10    12
2020-01-01  18:02:20    12
2020-01-01  18:02:30    7
2020-01-01  18:02:40    5
2020-01-01  18:02:50    3
2020-01-01  18:03:00    2
2020-01-01  18:03:10    2
2020-01-01  18:03:20    2
2020-01-01  18:03:30    2
2020-01-01  18:03:40    2
2020-01-01  18:03:50    2
2020-01-01  18:04:00    2

New Data Frame:

Date        Time        On_Off
2020-01-01  18:01:00    1
2020-01-01  18:02:30    0

To do so, I believe that I need to create an empty data frame, and then populate it with the extracted times from the original. Thanks!

Upvotes: 1

Views: 75

Answers (1)

Greg
Greg

Reputation: 3660

You can use lead and lag from dplyr to check for changes in Temp and top_n (also from dplyr) to get the first instance.

df %>%
  mutate(On_Off = case_when(Temp > lag(Temp) ~ "1",
                            Temp < lag(Temp) ~ "0",
                            TRUE ~ "No_Change")) %>% 
  filter(Temp > lag(Temp, default = 1) + 3 | Temp < lag(Temp) - 3) %>% 
  group_by(On_Off) %>% 
  top_n(1, wt = desc(Time)) %>% 
  select(Date, Time, On_Off)
# A tibble: 2 x 3
# Groups:   On_Off [2]
  Date       Time     On_Off
  <date>     <time>   <chr> 
1 2020-01-01 18:01:00 1     
2 2020-01-01 18:02:30 0     

Upvotes: 1

Related Questions