This tutorial explains how to apply functions on rows.
Sample Data
Apply Function
When we want to apply a function to the rows or columns of a matrix or data frame. It cannot be applied on lists or vectors.
Calculate maximum value across row
Count unique values across row
Sample Data
data = read.table(text="
XYZ
650
63NA
615
853
1NA 1
872
202", header=TRUE)
Apply Function
When we want to apply a function to the rows or columns of a matrix or data frame. It cannot be applied on lists or vectors.
![]() |
apply arguments |
Calculate maximum value across row
apply(data, 1, max)It returns NA if NAs exist in a row. To ignore NAs, you can use the following line of code.
apply(data, 1, max, na.rm = TRUE)Calculate mean value across row
apply(data, 1, mean)Calculate number of 0s in each row
apply(data, 1, mean, na.rm = TRUE)
apply(data == 0, 1, sum, na.rm= TRUE)Calculate number of values greater than 5 in each row
apply(data > 5, 1, sum, na.rm= TRUE)Select all rows having mean value greater than or equal to 4
df = data[apply(data, 1, mean, na.rm = TRUE)>=4,]Remove rows having NAs
helper = apply(data, 1, function(x){any(is.na(x))})It can be easily done with df2 = na.omit(data).
df2 = data[!helper,]
Count unique values across row
df3 = apply(data,1, function(x) length(unique(na.omit(x))))