handling NA values in apply functions returning more than one value

2.1k views Asked by At

I have dataframe df with two columns col1, col2, includes NA values in them. I have to calculate mean, sd for them. I have calculated them separately with below code.

# Random generation
set.seed(12)
df <- data.frame(col1 = sample(1:100, 10, replace=FALSE), 
                 col2 = sample(1:100, 10, replace=FALSE))

# Introducing null values
df$col1[c(3,5,9)] <- NA
df$col2[c(3,6)] <- NA

# sapply with return a value for a function
stat <- data.frame(Mean=numeric(length = length(df)), row.names = colnames(df))
stat[,'Mean'] <- as.data.frame(sapply(df, mean, na.rm=TRUE))
stat[,'Sd'] <- as.data.frame(sapply(df, sd, na.rm=TRUE))

I have tried to do both operations at a single time using the below code.

#sapply with return more than one value
stat[,c('Mean','Sd')] <- as.data.frame(t(sapply(c(1:length(df)),function(x)
    return(c(mean(df[,x]), sd(df[,x]))))))

As I failed to remove the NA values in the latest function, I am getting output as NA for both mean, sd.

Can you please give an idea on how to remove NA values for each function mean, sd. Also, please suggest any other possible smart ways to this.

1

There are 1 answers

4
BrodieG On BEST ANSWER

Here is an option:

funs <- list(sd=sd, mean=mean)
sapply(funs, function(x) sapply(df, x, na.rm=T))

Produces:

           sd       mean    
col1.value 39.34826 39.42857
col2.value 28.33946 51.625  

If you want to get cute with the functional library:

sapply(funs, Curry(sapply, X=df), na.rm=T)

Does the same thing.