Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter the middle row of each group

Tags:

r

filter

dplyr

df <- data.frame(id=c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3), 
             x=c(1,2,3,3,5,1,4,3,1,2,8,9,3,1,0))

For each group, if I want to filter the first row

 df %>%
 group_by(id) %>%
filter(row_number()==1)

What do I do if I have to filter the most middle row?

like image 495
89_Simple Avatar asked Apr 18 '18 11:04

89_Simple


People also ask

How do I filter a table based on positions of rows?

Power Query has multiple options to filter a table based on the positions of its rows, either by keeping or removing those rows. This article covers all the available methods. The keep rows set of functions will select a set of rows from the table and remove any other rows that don't meet the criteria.

How to filter rows by list of values in Excel?

Filter rows by list of values with Advanced Filter feature. We can apply the Advanced Filter feature to filter rows by a given list of values easily in Excel. Please do as follows: 1. Click Data > Advanced to open the Advanced Filter dialog box. 2.

What is filters and grouping in Excel?

In this tutorial, let’s have a brief idea about what is filters and grouping in Excel. Groupings in Excel helps to hide the rows and columns of the data records and it also helps in summarizing the grouped data records. Grouping in MS-excel is one of the tactics each and every excel expert and financial analyst should know.

How to include only the first 2 columns in a filter?

In situation when you want some neighboring columns to appear in a FILTER result, include only those columns in array because it is this argument that determines which columns to return. In the basic FILTER formula example, supposing you wish to return the first 2 columns ( Name and Group ).


2 Answers

n()/2 halves the nrow of the group, ceiling rounds up decimals for odd values.

df %>%
  group_by(id) %>%
  filter(row_number()==ceiling(n()/2))

# A tibble: 3 x 2
# Groups:   id [3]
     id     x
  <dbl> <dbl>
1     1     3
2     2     3
3     3     3
like image 59
LAP Avatar answered Oct 18 '22 11:10

LAP


You can also use slice rather than row_number() and do use n() to capture the length of each group. Divide that by 2 to get the middle value of each group.

library(dplyr)

df %>% 
 group_by(id) %>% 
 slice(ceiling(n()/2))
like image 25
Sotos Avatar answered Oct 18 '22 09:10

Sotos