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.
Here is an option:
Produces:
If you want to get cute with the
functional
library:Does the same thing.