Return Column Name of Largest Value for Each Row in R (Example)

 

This tutorial illustrates how to find the variables with the largest values in each row in the R programming language.

The content of the post looks as follows:

Let’s start right away!

 

Example Data

First, we’ll need to create some example data:

data <- data.frame(x1 = c(0, 1, 0, 7, 1),             # Creating example data
                   x2 = c(5, 0, 0, 0, 0),
                   x3 = 3)
data                                                  # Return example data
# x1 x2 x3
# 1  0  5  3
# 2  1  0  3
# 3  0  0  3
# 4  7  0  3
# 5  1  0  3

Have a look at the previous output of the RStudio console. It shows that our example data consists of five rows and three numeric columns.

 

Example: Return Column Names with Highest Number in Row Using colnames & max.col Functions

In this Example, I’ll explain how to find the variable names with the largest value by row. For this, we are using the colnames and the max.col functions as shown below:

colnames(data)[max.col(data, ties.method = "first")]  # Apply colnames & max.col functions
# "x2" "x3" "x3" "x1" "x3"

The previous R code returns a character vector containing the column names of the largest values of each row. Looks good!

 

Video, Further Resources & Summary

Do you want to learn more about data inspection in R? Then you may watch the following video of my YouTube channel. In the video instruction, I’m explaining the R programming code of this article.

 

The YouTube video will be added soon.

 

Besides that, you could read the related articles on www.statisticsglobe.com. A selection of articles about data manipulation and analysis can be found below:

 

In this tutorial you learned how to identify the highest numbers within each line of a data frame in R programming. Let me know in the comments section, in case you have further comments or questions.

 



Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top