Find the frequency of non-NA values in each row of an R dataframe.


To find the frequency of non-NA values in each row of an R data frame, we can use apply function along with na.omit function.

For Example, if we have a data frame called df that contains some NA values then we can find the frequency of non-NA values in each row of df by using the command as follows −

apply(df,1, function(x) length(na.omit(x)))

Example 1

Following snippet creates a sample data frame −

x1<-sample(c(NA,2,5),20,replace=TRUE)
x2<-sample(c(NA,10,15),20,replace=TRUE)
x3<-sample(c(NA,1,3),20,replace=TRUE)
df1<-data.frame(x1,x2,x3)
df1

The following dataframe is created

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

To find the frequency of non-NA values in each row of df1 on the above created data frame, add the following code to the above snippet −

x1<-sample(c(NA,2,5),20,replace=TRUE)
x2<-sample(c(NA,10,15),20,replace=TRUE)
x3<-sample(c(NA,1,3),20,replace=TRUE)
df1<-data.frame(x1,x2,x3)
apply(df1,1, function(x) length(na.omit(x)))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 3 3 2 3 2 1 2 2 1 2 1 3 3 2 2 1 1 2 3 2

Example 2

Following snippet creates a sample data frame −

y1<-sample(c(NA,round(rnorm(3),2)),20,replace=TRUE)
y2<-sample(c(NA,round(rnorm(3,10,2),2)),20,replace=TRUE)
y3<-sample(c(NA,round(rnorm(3,25,3.2),2)),20,replace=TRUE)
df2<-data.frame(y1,y2,y3)
df2

The following dataframe is created

      y1    y2    y3
1   0.27    NA 24.02
2   0.27  9.30 24.02
3   0.49    NA    NA
4     NA  8.10 25.60
5   0.49 11.11 29.34
6  -0.16    NA 25.60
7   0.49  9.30 29.34
8  -0.16  8.10 25.60
9  -0.16 11.11 24.02
10  0.49  8.10 29.34
11    NA 11.11 25.60
12  0.27  9.30 29.34
13 -0.16    NA    NA
14  0.27  8.10 25.60
15    NA  8.10 29.34
16  0.27    NA    NA
17 -0.16  9.30 24.02
18    NA  9.30    NA
19  0.49 11.11 25.60
20  0.49    NA 25.60

To find the frequency of non-NA values in each row of df2 on the above created data frame, add the following code to the above snippet −

y1<-sample(c(NA,round(rnorm(3),2)),20,replace=TRUE)
y2<-sample(c(NA,round(rnorm(3,10,2),2)),20,replace=TRUE)
y3<-sample(c(NA,round(rnorm(3,25,3.2),2)),20,replace=TRUE)
df2<-data.frame(y1,y2,y3)
apply(df2,1, function(x) length(na.omit(x)))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 2 3 1 2 3 2 3 3 3 3 2 3 1 3 2 1 3 1 3 2

Example 3

Following snippet creates a sample data frame −

z1<-sample(c(NA,runif(2,1,5)),20,replace=TRUE)
z2<-sample(c(NA,runif(2,5,10)),20,replace=TRUE)
z3<-sample(c(NA,runif(3,1,2)),20,replace=TRUE)
df3<-data.frame(z1,z2,z3)
df3

The following dataframe is created

         z1       z2       z3
1  2.237191       NA 1.242077
2  1.137563 8.614818 1.120255
3        NA 9.840686       NA
4  2.237191 9.840686 1.242077
5  1.137563       NA       NA
6        NA 9.840686       NA
7  1.137563       NA 1.242077
8  2.237191 9.840686 1.877137
9  1.137563 8.614818 1.120255
10 2.237191 9.840686       NA
11 1.137563 8.614818 1.877137
12       NA 8.614818 1.877137
13       NA       NA 1.877137
14 1.137563 8.614818       NA
15       NA 9.840686 1.242077
16 1.137563 9.840686 1.877137
17 2.237191 9.840686       NA
18 1.137563 8.614818 1.242077
19       NA 8.614818 1.120255
20 2.237191       NA 1.242077

To find the frequency of non-NA values in each row of df2 on the above created data frame, add the following code to the above snippet −

z1<-sample(c(NA,runif(2,1,5)),20,replace=TRUE)
z2<-sample(c(NA,runif(2,5,10)),20,replace=TRUE)
z3<-sample(c(NA,runif(3,1,2)),20,replace=TRUE)
df3<-data.frame(z1,z2,z3)
apply(df3,1, function(x) length(na.omit(x)))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 2 3 1 3 1 1 2 3 3 2 3 2 1 2 2 3 2 3 2 2

Updated on: 03-Nov-2021

179 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements