Select First Row of Each Group in Data Frame in R (Example)
In this article you’ll learn how to extract the first data frame row by group in the R programming language.
The article consists of the following:
Let’s dive right into the programming part!
Creation of Example Data
Have a look at the example data below:
data <- data.frame(x1 = 1:10, # Example data x2 = letters[1:10], group = c(rep("G1", 3), rep("G2", 4), rep("G3", 3))) data # Return example data # x1 x2 group # 1 1 a G1 # 2 2 b G1 # 3 3 c G1 # 4 4 d G2 # 5 5 e G2 # 6 6 f G2 # 7 7 g G2 # 8 8 h G3 # 9 9 i G3 # 10 10 j G3
Have a look at the previous RStudio console output. It shows that our example data has ten rows and three columns. The last variable of our data is a grouping column, separating the data into three groups.
Example: Extracting First Row of Each Group Using duplicated Function
The following R programming code shows how to use the duplicated function to select only the top row of each group.
data[!duplicated(data$group), ] # Apply duplicated # x1 x2 group # 1 1 a G1 # 4 4 d G2 # 8 8 h G3
As you can see, only three rows of our original data table were kept.
Video & Further Resources
Do you want to learn more about the manipulation of data frames in R? Then you could watch the following video of my YouTube channel. In the video, I’m explaining the R syntax of this post.
The YouTube video will be added soon.
In addition, I can recommend to read the related tutorials of my homepage.
- Extract Row from Data Frame in R
- Extract First N Rows of Data Frame
- Extract Subset of Data Frame Rows Containing NA
- Split Data Frame in R
- Remove First Row of Data Frame
- The R Programming Language
In summary: This article illustrated how to return the top line of each data table group in the R programming language. In case you have additional questions, let me know in the comments below.
Statistics Globe Newsletter
2 Comments. Leave new
Hi, when group and x2 are mixed order, if you want to sort first and then get first values
Hey Yilmaz,
You can sort your data as explained here first, and then you can use the code of the present tutorial.
Regards,
Joachim