Learn how to find the max value in each row of a data frame or matrix in R using apply(), pmax(), dplyr, and matrixStats. Step-by-step examples, code explanations, and tips for handling missing values. Perfect for R programmers seeking efficient row-wise operations.
code
rtip
Author
Steven P. Sanderson II, MPH
Published
September 22, 2025
Keywords
Programming, find max value in row R, rowwise maximum R, R apply max row, dplyr row max, R matrixStats max row, how to get row max in R, R get maximum value per row, R dataframe row maximum, best way to find row max in R, max value by row in R dataframe
Key Insight: Finding the maximum value in each row is a common data analysis task, in R it’s simple. The apply() function with max() is the most straightforward method, but several alternatives offer better performance or integration with modern R workflows.
Finding the max value in each row is a useful operation in data analysis. Whether you’re analyzing exam scores, stock prices, or sensor measurements, knowing how to efficiently extract row-wise maximums can save you time and improve your data processing workflows. This guide covers four proven methods using apply(), pmax(), dplyr, and matrixStats packages.
Understanding Row-Wise Operations in R
Row-wise operations in R work across the columns of each row, rather than down the columns. When we want the max value in each row, we’re looking for the highest value across all columns for every single row in our dataset.
Basic Concept:
Column-wise: Operations down each column (like finding the mean of each column)
Row-wise: Operations across columns for each row (like finding the max of each row)
Method 1: Using apply() - The Most Common Approach
The apply() function is the most popular method for finding row maximums in R. It’s part of base R, so no additional packages are required.
• Forgetting na.rm = TRUE: Returns NA if any value in row is missing - Solution: Always use na.rm = TRUE when dealing with missing data
• Wrong MARGIN parameter: Using MARGIN = 2 finds column max, not row max - Solution: Remember 1 = rows, 2 = columns
• All-NA rows: With na.rm = TRUE, returns -Inf instead of NA - Solution: Use custom function to check for all-NA rows
• Character columns:max() doesn’t work on text data - Solution: Select only numeric columns first
Your Turn!
Practice Exercise:
Create a data frame with sales data for different products across four months and find the best performing month for each product.
# Your challenge datasales_data <-data.frame(Product =c("Laptop", "Phone", "Tablet", "Watch"),Jan =c(1200, 800, 600, 300),Feb =c(1100, 850, 550, 350),Mar =c(1300, 900, 700, 400),Apr =c(1250, 820, 650, 380))# TODO: Find the maximum sales for each product# TODO: Find which month had the highest sales for each product
Click here for Solution!
# Solution 1: Find maximum salessales_data$Best_Sales <-apply(sales_data[2:5], 1, max)# Solution 2: Find best monthsales_data$Best_Month <-apply(sales_data[2:5], 1, function(x) {names(x)[which.max(x)]})# Alternative using multiple methodssales_data$Max_pmax <-pmax(sales_data$Jan, sales_data$Feb, sales_data$Mar, sales_data$Apr)print(sales_data)
Product Jan Feb Mar Apr Best_Sales Best_Month Max_pmax
1 Laptop 1200 1100 1300 1250 1300 Mar 1300
2 Phone 800 850 900 820 900 Mar 900
3 Tablet 600 550 700 650 700 Mar 700
4 Watch 300 350 400 380 400 Mar 400
Key Takeaways
• apply(df, 1, max) is the most common and versatile method for finding row maximums • Use na.rm = TRUE when your data contains missing values • pmax() is faster for datasets with few columns • matrixStats::rowMaxs() provides the best performance for large datasets • dplyr::rowwise() integrates well with tidyverse workflows • Always specify MARGIN = 1 in apply() for row-wise operations
Conclusion
Finding the max value in each row in R can be accomplished through multiple approaches, each with specific advantages. The apply() function remains the gold standard for most users due to its simplicity and reliability. For performance-critical applications, consider matrixStats::rowMaxs(), while tidyverse users will appreciate dplyr’s readable syntax.
Choose the method that best fits your workflow, data size, and coding style. With these techniques, you’ll efficiently handle row-wise maximum calculations in any R project.
Ready to level up your R skills? Try implementing these methods with your own datasets and see which approach works best for your specific use case!