What is the maximum value in each row in R?

What is the maximum value in each row in R?

The maximum value in each row in R refers to the largest numerical value present in a particular row of a dataset in the programming language R. This value is determined by comparing all the numbers in the row and selecting the one with the highest numerical value. It is a useful metric for identifying the overall highest value in a dataset and can be used for various analytical and statistical purposes.

Find the Max Value in Each Row in R


You can use the following basic syntax to find the max value in each row of a data frame in R:

df$max <- apply(df, 1, max, na.rm=TRUE)

This particular syntax creates a new column called max that contains the max value in each row of the data frame.

The following example shows how to use this syntax in practice.

Example: Find the Max Value in Each Row in R

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(points=c(4, NA, 10, 2, 15, NA, 7, 22),
                 rebounds=c(NA, 3, 9, 7, 6, 8, 14, 10),
                 assists=c(10, 9, 4, 4, 3, 7, 10, 11))

#view data frame
df

  points rebounds assists
1      4       NA      10
2     NA        3       9
3     10        9       4
4      2        7       4
5     15        6       3
6     NA        8       7
7      7       14      10
8     22       10      11

We can use the following syntax to create a new column called max that contains the max value in each row:

#add new column that contains max value in each row
df$max <- apply(df, 1, max, na.rm=TRUE)

#view updated data frame
df

  points rebounds assists max
1      4       NA      10  10
2     NA        3       9   9
3     10        9       4  10
4      2        7       4   7
5     15        6       3  15
6     NA        8       7   8
7      7       14      10  14
8     22       10      11  22

The new column called max contains the max value in each row.

Note: If you don’t include na.rm=TRUE within the apply() function, then NA values will be returned if they exist anywhere in the row.

Cite this article

stats writer (2024). What is the maximum value in each row in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-is-the-maximum-value-in-each-row-in-r/

stats writer. "What is the maximum value in each row in R?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/what-is-the-maximum-value-in-each-row-in-r/.

stats writer. "What is the maximum value in each row in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-is-the-maximum-value-in-each-row-in-r/.

stats writer (2024) 'What is the maximum value in each row in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-is-the-maximum-value-in-each-row-in-r/.

[1] stats writer, "What is the maximum value in each row in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. What is the maximum value in each row in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top