Skip to content

Data Science Tutorials

For Data Science Learners

  • Tips for Data Scientist Interview Openings
    Tips for Data Scientist Interview Openings Course
  • Remove Columns from a data frame
    How to Remove Columns from a data frame in R R
  • Mastering R Programming for Data Science: Tips and Tricks R
  • optim Function in R R
  • Add Footnote to ggplot2 R
  • Fisher’s exact test in R
    Fisher’s exact test in R R
  • Triangular Distribution in R
    Triangular Distribution in R R
  • How to Use Gather Function in R
    How to Use Gather Function in R?-tidyr Part2 R
Get the first value in each group in R

Get the first value in each group in R?

Posted on April 23April 30 By Admin No Comments on 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

R

Post navigation

Previous Post: Control Chart in Quality Control-Quick Guide
Next Post: How to Use the Multinomial Distribution in R?

Related Posts

  • OLS Regression in R
    OLS Regression in R R
  • one-sample-proportion-test-in-r
    One sample proportion test in R-Complete Guide R
  • bootstrapping-in-r
    Bootstrapping in R R
  • How to Find the Size of a Data Frame in R R
  • How to Specify Histogram Breaks in R R
  • How to Perform Bootstrapping in R
    How to Perform Bootstrapping in R R

Leave a Reply Cancel reply

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

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Triangular Distribution in R
    Triangular Distribution in R R
  • Ad Hoc Analysis
    What is Ad Hoc Analysis? Statistics
  • How to Create an Interaction Plot in R
    How to Create an Interaction Plot in R? R
  • How to Calculate Ratios in R
    How to Calculate Ratios in R R
  • The Multinomial Distribution in R
    The Multinomial Distribution in R R
  • Methods for Integrating R and Hadoop
    Methods for Integrating R and Hadoop complete Guide R
  • How to perform MANOVA test in R
    How to perform the MANOVA test in R? R
  • Error attempt to apply non function in r
    Error attempt to apply non function in r R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme