How to calculate row means by excluding NA values in an R data frame?


To find the row means we can use rowMeans function but if we have some missing values in the data frame then na.rm=TRUE argument can be used in the same way as it is used while calculating the means for columns. For example, if we have a data frame df that contains two columns x and y each having some missing values then the row means can be calculated as rowMeans(df,na.rm=TRUE).

Example

Consider the below data frame −

 Live Demo

set.seed(1515)
x1<-sample(c(NA,1,25,31),20,replace=TRUE)
x2<-sample(c(NA,5,12,27),20,replace=TRUE)
x3<-sample(c(NA,15),20,replace=TRUE)
x4<-sample(c(NA,15,9),20,replace=TRUE)
df1<-data.frame(x1,x2,x3,x4)
df1

Output

  x1 x2 x3 x4
1 25 NA NA NA
2 25 12 15 NA
3 25 NA 15 NA
4 31 5 NA NA
5 31 27 15 15
6 NA 5 NA 9
7 25 12 15 NA
8 31 5 15 NA
9 1 5 15 15
10 1 27 NA NA
11 25 NA 15 NA
12 25 12 15 15
13 25 NA 15 9
14 31 NA 15 15
15 31 27 15 9
16 1 12 NA 15
17 1 NA NA 9
18 25 27 15 NA
19 31 5 15 9
20 NA 5 15 NA

Finding the row means of df1 −

rowMeans(df1,na.rm=TRUE)

Output

[1]  25.000000 17.333333 20.000000 18.000000 22.000000 7.000000 17.333333
[8]  17.000000 9.000000 14.000000 20.000000 16.750000 16.333333 20.333333
[15] 20.500000 9.333333 5.000000 22.333333 15.000000 10.000000

Let’s have a look at another example −

Example

 Live Demo

y1<-sample(c(NA,rnorm(5,1,0.003)),20,replace=TRUE)
y2<-sample(c(NA,rnorm(10,50,2.47)),20,replace=TRUE)
y3<-sample(c(NA,runif(5,1,4)),20,replace=TRUE)
y4<-sample(c(NA,runif(5,2,10)),20,replace=TRUE)
y5<-sample(c(NA,rexp(5,3.5)),20,replace=TRUE)
df2<-data.frame(y1,y2,y3,y4,y5)
df2

Output

y1 y2 y3 y4 y5
1 0.9965744 48.73434 2.097240 9.657755 0.32815971
2 1.0003618 44.83392 2.877004 9.735341 0.27053003
3 0.9974534 NA 2.097240 9.657755 0.64288668
4 0.9999057 54.12249 2.097240 NA 0.06486254
5 1.0003618 54.12249 2.877004 5.945301 NA
6 0.9965744 NA NA NA 0.27053003
7 1.0003618 54.12249 NA 5.945301 0.06486254
8 1.0022832 44.83392 1.065712 5.945301 0.64288668
9 1.0003618 54.34290 NA 9.735341 0.64288668
10 1.0003618 NA 2.323069 3.774950 NA
11 0.9999057 54.12249 1.834897 3.774950 0.64288668
12 0.9999057 53.84937 1.834897 NA 0.44797666
13 0.9974534 47.75855 1.065712 9.735341 0.44797666
14 1.0022832 NA 1.065712 3.774950 0.32815971
15 1.0003618 54.12249 2.877004 5.945301 0.27053003
16 0.9974534 54.34290 2.323069 9.657755 0.64288668
17 NA 44.83392 1.065712 3.774950 0.32815971
18 0.9965744 54.34290 NA NA 0.06486254
19 1.0022832 49.89409 2.323069 3.774950 0.06486254
20 1.0003618 49.89409 1.065712 4.078849 0.32815971

Finding the row means of df2 −

Example

rowMeans(df2,na.rm=TRUE)

Output

[1]  12.3628143 11.7434319 3.3488338 14.3211253 15.9862898 0.6335522
[7]  15.2832544 10.6980210 16.4303723 2.3661269 12.2750266 14.2830369
[13] 12.0010071 1.5427764 12.8431379 13.5928126 12.5006862 18.4681122
[19] 11.4118515 11.2734351

Updated on: 17-Oct-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements