处理返回多个值的应用函数中的NA值 [英] handling NA values in apply functions returning more than one value
问题描述
我有数据框 df
,其中有两列 col1
, col2
,其中包含 NA
值。我必须为他们计算意味着
, sd
。我已经用下面的代码分别计算了它们。
#随机生成
set.seed(12)
df< - data.frame(col1 = sample(1:100,10,replace = FALSE),
col2 = sample(1:100,10,replace = FALSE))
#引入空值
df $ col1 [c(3,5,9)]< - NA
df $ col2 [c(3,6)]< - NA
#sapply返回一个函数的值
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))
我一直尝试两次操作使用以下代码。
#sapply返回多个值
stat [,c('Mean' 'Sd')]< - as.data.frame(t(sap(c(1:length(df)),function(x)
return(c(mean(df [,x])) (df [,x])))))))
最新功能中的 NA
值,我的输出为 NA
/ code>,
, sd
。您可以提出如何删除 NA
每个函数的值 c> c sd
。另外,请建议其他任何可能的智能方式。
这是一个选项:
funs< - list(sd = sd,mean = mean)
sapply(funs,function(x)sapply(df,x, rm = T))
产生:
sd意味着
col1.value 39.34826 39.42857
col2.value 28.33946 51.625
如果您想要使用功能库可爱:
sapply(fun,Curry(sapply,X = df),na.rm = T)
同样的事情。
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:
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.
这篇关于处理返回多个值的应用函数中的NA值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!