Finding value +1 within a group

Emma Doughty

I have a large dataframe that I have grouped by Date. Within each date group, I'd like to keep the rows for rooms that are next door to one another, i.e. Room.num = any Room.num in group +/- 1

 Date        Room.num  
1 2019.08.21  1  
2 2019.08.21  2  
3 2020.07.18  6  
4 2020.07.18  1  
5 2020.07.18  3

I want to end up with just

  Date        Room.num  
1 2019.08.21  1  
2 2019.08.21  2 
  1. I have tried
df %>% 
   group_by(Date)
   filter(Room.num=1)

which gives me

Date Room.num
1 2019.08.21 1
4 2020.07.18 1

  1. I have also tried
df %>% 
   group_by(Date)
   filter(any(Room.num=1))

This gives me

  Date        Room.num  
1 2019.08.21  1  
2 2019.08.21  2  
3 2020.07.18  6  
4 2020.07.18  1  
5 2020.07.18  3

i.e. any group with a Room.num=1

  1. But I am not able to do what I want keep rows whichhave a room number that is +/- 1 of any other Room.num in that Date group
df %>% 
   group_by(Date)
   filter(Room.num==any(Room.num)+1)

I get

Date        Room.num  
2 2019.08.21  2  

The second Room.num is always = 1

AnilGoyal

I think you can do this in tidyverse

df <- read.table(header = T, text = 'Date Room.num
1 2019.08.21 1
2 2019.08.21 2
3 2020.07.18 6
4 2020.07.18 1
5 2020.07.18 3
')

library(tidyverse)
df  %>%
  group_by(Date) %>%
  filter(map_lgl(Room.num, ~ .x %in% c(Room.num -1, Room.num +1))) %>%
  ungroup()
#> # A tibble: 2 x 2
#>   Date       Room.num
#>   <chr>         <int>
#> 1 2019.08.21        1
#> 2 2019.08.21        2

If you, however, want to avoid purrr::map_lgl use this similar baseR function like this

df  %>%
  group_by(Date) %>%
  filter(unlist(Map(\(.x) .x %in% c(Room.num -1, Room.num +1), Room.num))) %>%
  ungroup()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL query for finding records where members within each group are the same

Finding the minimum and maximum value within a Metal texture

Finding the difference between the consecutive lines within group in R

Finding nearest value within a data.table

Even splitting of a group to within 1%

Finding the Min/Max value within a Struct

Change value by group based in reference within group

Finding value within a div class

Finding rows with MAX value after using GROUP BY

Finding the closest geographical point within each group

Finding certain pixel value within image

Purrr In R finding last row within a group that meets a criteria and then pulling a value from that row

finding minimum value between 1 value and a column

Finding the lowest value within an instance variable

Mongo query - finding minimum value within a document

Finding rows with the maximum values within a group

Finding an ID with the maximum value of an attribute in a group with SQL

jQuery picking out value but not showing/finding it within

DB2 SQL: within group, assign value of column 2 when column 1 is at maximum, to each row

Finding the minimum value that comes after the maximum value in a group within a Pandas dataframe

Finding the max value in group with differentiation

Finding the maximum value in a group with differentiation

Finding the closest values within mutil group

Finding value inside an object, that's within an array

How to use an existing dummy variable to create a new one that takes the value 1 for certain lead observations within a group

How to create a dummy variable that takes the value 1 if all values of a variable within a group exceed a certain value

Finding maximum value in group

Finding number of events in previous winter within group

Finding min value by group