LPS
LPS

Reputation: 23

How to remove the last N rows on a dataset based on ID in R?

How do I go from a dataframe like this:

ID X Y
1 4 6
1 6 5
1 8 4
1 9 6
2 6 4
2 7 5
2 3 9

to this:

ID X Y
1 4 6
1 6 5
2 6 4

In this example, I wanted to remove the last 2 rows for every ID.

Upvotes: 0

Views: 60

Answers (3)

akrun
akrun

Reputation: 887048

An option with head used within slice

library(dplyr)
df %>% 
   group_by(ID) %>% 
   slice(head(row_number(), -2)) %>% 
   ungroup

-output

# A tibble: 3 x 3
     ID     X     Y
  <int> <int> <int>
1     1     4     6
2     1     6     5
3     2     6     4

data

df <- structure(list(ID = c(1L, 1L, 1L, 1L, 2L, 2L, 2L), X = c(4L, 
6L, 8L, 9L, 6L, 7L, 3L), Y = c(6L, 5L, 4L, 6L, 4L, 5L, 9L)), 
  class = "data.frame", row.names = c(NA, 
-7L))

Upvotes: 3

TarJae
TarJae

Reputation: 78927

You can use:

library(dplyr)
 df %>% 
  group_by(ID) %>% 
  filter(row_number() <= n()-2)

Output:

     ID     X     Y
  <int> <int> <int>
1     1     4     6
2     1     6     5
3     2     6     4

Upvotes: 2

Anoushiravan R
Anoushiravan R

Reputation: 21908

You can use the following solution:

library(dplyr)

df %>% 
  group_by(ID) %>%
  filter(between(row_number(), 1, n()-2))

# A tibble: 3 x 3
# Groups:   ID [2]
     ID     X     Y
  <int> <int> <int>
1     1     4     6
2     1     6     5
3     2     6     4

Or this one:

df %>% 
  group_by(ID) %>%
  slice(1:(n()-2))

# A tibble: 3 x 3
# Groups:   ID [2]
     ID     X     Y
  <int> <int> <int>
1     1     4     6
2     1     6     5
3     2     6     4

Upvotes: 3

Related Questions