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.
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))
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With