Get the first value in each group in R?

Get the first value in each group in R, Knowing the first, last, or nth value in the group can be important at times. With the help of various examples, we will look at how to retrieve the initial value for each group in this article.

The aggregate() or group by() functions in R can be used to get the first value in each group. Let’s have a look at how toGroup by a single column to get the first value of each group.

Get each group’s initial value – group by multiple columns

Let’s start by creating a data frame that we’ll use to demonstrate the examples throughout this lesson.

df<-data.frame(Name=c('A','B','C','D','E','F','G','H','I','J','K','L'),
State=c('S1','S1','S2','S2','S3','S3','S3','S4','S4','S4','S4','S4'),
Sales=c(124,224,231,212,123,71,39,131,188,186,198,134))
df

The data frame will look like

 Name State Sales
1     A    S1   124
2     B    S1   224
3     C    S2   231
4     D    S2   212
5     E    S3   123
6     F    S3    71
7     G    S3    39
8     H    S4   131
9     I    S4   188
10    J    S4   186
11    K    S4   198
12    L    S4   134

We now have a data frame containing the Sales scores of 12 Products across their multiple states.

Approach 1:

The aggregate function, which is categorized by state and name, is discussed, as well as the function first, which is used to acquire the first value of each group.

Let’s make use of the aggregate function in R

aggregate(df$Sales, by=list(df$State), FUN=first)

The data frame will be

 Group.1   x
1      S1 124
2      S2 231
3      S3 123
4      S4 131

Approach 2:

Using the dplyr package’s group by() method

Load dplyr package and we can make use of the same data frame as we mentioned earlier.

library(dplyr)
df %>% group_by(State) %>% summarise(First_value= first(Sales))

The data frame will be

State First_value
  <chr>       <dbl>
1 S1            124
2 S2            231
3 S3            123
4 S4            131

Great, because both approaches yielded the same result.

How to get the last value of each group in R – Data Science Tutorial

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

fourteen − two =