Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping/removing last/first row in each group R

Tags:

r

group-by

Here is my dataframe:

categ <- c('a','a','a','b','b')
value <- c(1,2,5,4,5)
df <- data.frame(categ, value)

I would like to group by categ column and drop the first/last element in each group. Ideally I would like to create a column with indication of the first/last element in each group. Here is what I tried, but it did not work:

library('plyr')
ddply(df, .(categ), function(x) x[-nrow(x)])

P.S. This is probably duplicate question, but I am new to R and R is very cryptic, so I cannot port solutions immediately.

like image 673
user1700890 Avatar asked Dec 09 '16 22:12

user1700890


3 Answers

There is an even simpler solution using {dplyr} to drop the first row of each group:

library(dplyr)    
df %>% 
       group_by(categ) %>% 
       slice(2:n())

For dropping last row of each group:

df %>% 
    group_by(categ) %>% 
    slice(1:(n()-1))
like image 126
Tamas Nagy Avatar answered Oct 12 '22 01:10

Tamas Nagy


You may use the dplyr package instead. Following code drops the last element of each group.

    library(dplyr)
    df %>% 
      dplyr::group_by(categ) %>% 
      dplyr::mutate(rank = 1:length(value)) %>% 
      dplyr::filter(rank < max(rank)) %>% 
      dplyr::mutate(rank = NULL)
like image 41
Simon Müller Avatar answered Oct 12 '22 02:10

Simon Müller


Think you were just missing a comma to signify that you wanted rows rather than the default for "[" which is column selection. For dropping the last row:

ddply(df, .(categ), function(x) x[-nrow(x), ])

Could also have used the head-function. For dropping the first item, the tail function can be substituted for head:

> ddply(df, .(categ), function(x) head(x ,-1) )
  categ value
1     a     1
2     a     2
3     b     4

Noting that plyr and dplyr don't play well together, I am now restarting R.

like image 42
IRTFM Avatar answered Oct 12 '22 03:10

IRTFM