# 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.

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.

Subscribe to the Statistics Globe Newsletter

• Abigail Drummond
June 15, 2021 6:05 pm

Hi, I am trying to do this but each time it returns a column that is a character, rather than the numeric columns. How can I fix this? Thanks.

• June 16, 2021 9:03 am

Hey Abigail,

Thank you for the comment!

Could you illustrate how your data looks like and could you share the code you have used?

Regards

Joachim

• Jan Müller
June 15, 2022 11:48 am

Thanks already for your tutorial, how do I use this code if i want only specific columns? Let´s say column 4-19?
Thanks so much!

• June 15, 2022 1:01 pm

Hey Jan,

Thanks for the kind comment.

You may apply this syntax to a subset of your data frame. Consider the example code below:

`colnames(data[ , 4:19])[max.col(data[ , 4:19], ties.method = "first")]`

Regards,
Joachim