How to find the row sums if NA exists in the R data frame?

R ProgrammingServer Side ProgrammingProgramming

To find the row sums if NA exists in the R data frame, we can use rowSums function and set the na.rm argument to TRUE and this argument will remove NA values before calculating the row sums.

For Example, if we have a data frame called df that contains some NA values then we can find the row sums by using the below command −

df$Row_Sums<-rowSums(df,na.rm=TRUE)

Example 1

Following snippet creates a sample data frame −

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

The following dataframe is created

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

To find row sums for each row in df1 by ignoring NA values 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,2,5),20,replace=TRUE)
x3<-sample(c(NA,2,5),20,replace=TRUE)
df1<-data.frame(x1,x2,x3)
df1$Row_Sums<-rowSums(df1,na.rm=TRUE)
df1

Output

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

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

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),2)),20,replace=TRUE)
y3<-sample(c(NA,round(rnorm(3),2)),20,replace=TRUE)
df2<-data.frame(y1,y2,y3)
df2

The following dataframe is created

      y1    y2    y3
1  -0.27    NA    NA
2  -0.27    NA -0.70
3  -0.33    NA -0.06
4  -0.27 -1.35 -0.09
5   0.23 -2.03 -0.09
6     NA -2.03 -0.70
7  -0.27    NA    NA
8  -0.27 -2.03 -0.09
9  -0.27    NA -0.06
10 -0.33    NA -0.70
11 -0.27    NA -0.70
12    NA    NA    NA
13 -0.33    NA    NA
14    NA -1.35 -0.70
15    NA -0.93 -0.06
16  0.23 -1.35 -0.06
17  0.23 -0.93 -0.06
18 -0.27 -0.93 -0.06
19 -0.33    NA -0.06
20    NA -0.93 -0.09

To find row sums for each row in df2 by ignoring NA values 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),2)),20,replace=TRUE)
y3<-sample(c(NA,round(rnorm(3),2)),20,replace=TRUE)
df2<-data.frame(y1,y2,y3)
df2$Row_Sums<-rowSums(df2,na.rm=TRUE)
df2

Output

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

        y1    y2    y3 Row_Sums
1    -0.27    NA    NA -0.27
2    -0.27    NA -0.70 -0.97
3    -0.33    NA -0.06 -0.39
4    -0.27 -1.35 -0.09 -1.71
5     0.23 -2.03 -0.09 -1.89
6       NA -2.03 -0.70 -2.73
7    -0.27    NA    NA -0.27
8    -0.27 -2.03 -0.09 -2.39
9    -0.27    NA -0.06 -0.33
10   -0.33    NA -0.70 -1.03
11   -0.27    NA -0.70 -0.97
12      NA    NA    NA  0.00
13   -0.33    NA    NA -0.33
14      NA -1.35 -0.70 -2.05
15      NA -0.93 -0.06 -0.99
16    0.23 -1.35 -0.06 -1.18
17    0.23 -0.93 -0.06 -0.76
18   -0.27 -0.93 -0.06 -1.26
19   -0.33    NA -0.06 -0.39
20      NA -0.93 -0.09 -1.02
raja
Published on 03-Nov-2021 05:15:03
Advertisements