Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a column with count of NAs and Mean

Tags:

r

na

dplyr

I have a data frame and I need to add another column to it which shows the count of NAs in all the other columns for that row and also the mean of the non-NA values. I think it can be done in dplyr.

> df1 <- data.frame(a = 1:5, b = c(1,2,NA,4,NA), c = c(NA,2,3,NA,NA))
> df1
  a  b  c
1 1  1 NA
2 2  2  2
3 3 NA  3
4 4  4 NA
5 5 NA NA

I want to mutate another column which counts the number of NAs in that row and another column which shows the mean of all the NON-NA values in that row.

like image 534
sachinv Avatar asked Feb 16 '16 21:02

sachinv


People also ask

How do I count NAS in each column in R?

The best way to count the number of NA's in the columns of an R data frame is by using the colSums() function. As the name suggests, the colSums() function calculates the sum of all elements per column.

How do I count the number of NAS in a row in R?

1. Count the Number of NA's per Row with rowSums() The first method to find the number of NA's per row in R uses the power of the functions is.na() and rowSums(). Both the is.na() function and the rowSums() function are R base functions.

Which aggregate function counts the number of non NA values in the group?

The SAS function N calculates the number of non-blank numeric values across multiple columns.


1 Answers

library(dplyr)

count_na <- function(x) sum(is.na(x))    

df1 %>%
  mutate(means = rowMeans(., na.rm = T),
         count_na = apply(., 1, count_na))

#### ANSWER FOR RADEK ####
elected_cols <- c('b', 'c')

df1 %>%
  mutate(means = rowMeans(.[elected_cols], na.rm = T),
         count_na = apply(.[elected_cols], 1, count_na))
like image 76
maloneypatr Avatar answered Oct 11 '22 08:10

maloneypatr