R delete last row in dataframe for each group

aju_k

I would like to delete the last row in a dataframe for each group in R based on max(start_date).

Example data:

id      start_date  end_date
1       2016-01-14  2016-02-14
1       2016-03-14  2016-08-05
2       2014-01-14  2014-02-14
2       2015-03-21  2015-05-21
2       2015-08-23  2015-09-23
2       2015-11-21  2016-01-03

Result:

id      start_date  end_date
1       2016-01-14  2016-02-14
2       2014-01-14  2014-02-14
2       2015-03-21  2015-05-21
2       2015-08-23  2015-09-23

The following does not work:

df <- df %>% 
   group_by(id) %>% 
   summarise(start_date != max(start_date))

Error: found duplicated column name: id

df <- sqldf("select * from df group by id having start_date != max(start_date)")

error in statement: duplicate column name: id

Any suggestions would be great.

akrun

We can use slice (assuming that the dates are already ordered)

df1 %>% 
   group_by(id) %>% 
   slice(-n())
#     id start_date   end_date
#   <int>      <chr>      <chr>
#1     1 2016-01-14 2016-02-14
#2     2 2014-01-14 2014-02-14
#3     2 2015-03-21 2015-05-21
#4     2 2015-08-23 2015-09-23

If the dates are not ordered, then arrange and slice

df1 %>%
   group_by(id) %>%
   arrange(start_date) %>%
   slice(-n()) 

Based on some previous benchmarks (couldn't find the link), the arrange/slice method would be faster than comparing start_date != max(start_date)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pandas dataframe compare first and last row from each group

From Dev

Add each column with last value of last column of the row in dataframe R

From Dev

Dropping/removing last/first row in each group R

From Dev

R divide each column in dataframe by last row value

From Dev

R divide each column in dataframe by last row value

From Dev

Pandas dataframe get all rows between zero(0) of mask column and get first and last row of each group

From Dev

How to fillna the last row of each group in Pandas?

From Dev

MySQL: Select last row in each aggregate group

From Dev

Mysql select last row for each group

From Dev

Getting last row of each group in mysql

From Dev

MySQL: Select last row in each aggregate group

From Dev

Moving last two elements of each row of a dataframe

From Dev

min for each row with dataframe in R

From Java

Pandas dataframe get first row of each group

From Dev

Fill in the same value for each row by group in R

From Dev

drop first and last row from within each group

From Dev

Delete rows with specific values by keeping first and last row of dataframe

From Dev

Group and average dataframe rows based on a condition and by excluding last row

From Dev

Applying functions to each group in a dataframe in R

From Dev

R split each row of a dataframe into two rows

From Dev

R split each row of a dataframe into two rows

From Dev

Delete first row in Dataframe each day for certain value only

From Dev

How to multiply each element of a dataframe by sum of element in each row in R?

From Dev

In R, split a dataframe so subset dataframes contain last row of previous dataframe and first row of subsequent dataframe

From Dev

First row for each group

From Dev

Sum of group but keep the same value for each row in r

From Dev

Replace NA with values in another row of same column for each group in r

From Dev

Replace NA with values in another row of same column for each group in r

From Dev

Transform each group in a DataFrame

Related Related

  1. 1

    pandas dataframe compare first and last row from each group

  2. 2

    Add each column with last value of last column of the row in dataframe R

  3. 3

    Dropping/removing last/first row in each group R

  4. 4

    R divide each column in dataframe by last row value

  5. 5

    R divide each column in dataframe by last row value

  6. 6

    Pandas dataframe get all rows between zero(0) of mask column and get first and last row of each group

  7. 7

    How to fillna the last row of each group in Pandas?

  8. 8

    MySQL: Select last row in each aggregate group

  9. 9

    Mysql select last row for each group

  10. 10

    Getting last row of each group in mysql

  11. 11

    MySQL: Select last row in each aggregate group

  12. 12

    Moving last two elements of each row of a dataframe

  13. 13

    min for each row with dataframe in R

  14. 14

    Pandas dataframe get first row of each group

  15. 15

    Fill in the same value for each row by group in R

  16. 16

    drop first and last row from within each group

  17. 17

    Delete rows with specific values by keeping first and last row of dataframe

  18. 18

    Group and average dataframe rows based on a condition and by excluding last row

  19. 19

    Applying functions to each group in a dataframe in R

  20. 20

    R split each row of a dataframe into two rows

  21. 21

    R split each row of a dataframe into two rows

  22. 22

    Delete first row in Dataframe each day for certain value only

  23. 23

    How to multiply each element of a dataframe by sum of element in each row in R?

  24. 24

    In R, split a dataframe so subset dataframes contain last row of previous dataframe and first row of subsequent dataframe

  25. 25

    First row for each group

  26. 26

    Sum of group but keep the same value for each row in r

  27. 27

    Replace NA with values in another row of same column for each group in r

  28. 28

    Replace NA with values in another row of same column for each group in r

  29. 29

    Transform each group in a DataFrame

HotTag

Archive