假设我要为每行或每组计算一个特定值(在此示例中,该值是一个平均值)我将如何做?
Personal_mean <- function(observations, data, na.rm){
#Take obs from data
observations <- eval(substitute(observations), envir=data)
#add the observations together
sum_of_obs <- sum(observations, na.rm=na.rm)
#adjusts for NA in data
if (na.rm==TRUE){
length_of_obs <- length(observations[!is.na(observations)])
} else{
length_of_obs <- length(observations)
}
# applies above calculations per observation of the object
m<-0
for(i in 1:length(observations[!is.na(observations)])){
m[i]<- sum_of_obs[i]/length_of_obs[i]
}
return(m)
}
我想用以下示例数据测试此功能:
testdata <- data.frame(Gr,O1,O2,O3)
Gr <- c("G1","G2","G3","G4","G5")
O1 <- c(5,7,10,3.5,5)
O2 <- c(4,8,20,NA,3)
O3 <- c(6,NA,18,5.5,2)
因此,这应得出5、7.5、16、4.5和3.3的均值 基本上,我希望该函数执行的操作是计算每行或每组的平均值,能够处理数据中的NA,并能够从提供的数据中进行观察。 每当我尝试此代码时,都会得到以下结果:
[1] 7.461538 NA NA NA NA NA NA NA NA NA
[11] NA NA NA
那我在做什么错了,它不起作用了?
提前谢谢!
答案 0 :(得分:0)
尽管不是最佳选择,但这是另一种选择。您可以根据需要进行重构:
my_mean<-function(data,func,observations,na.rm=F,exc){
m<-as.data.frame(mget(observations,envir = as.environment(data)))
if(na.rm==T){
#m[is.na(m)]<-as.numeric(0)
res1<-apply(m[complete.cases(m),],1,func)
m<-m[!complete.cases(m),]
ms[is.na(ms)]<-0
res2<-rowSums(ms[,-exc])/2
return(list(res1=res1,res2=res2))
#res2 contains mean for rows with NAs
#exc is the column to exclude ie has non numeric data
#res1 has mean for all rows with no NAs
}else{
apply(m,1,func)
}
}
测试:
my_mean(testdata,mean,c("O1","O2","O3"),na.rm = F,1)
# $res1
# 1 3 5
# 5.000000 16.000000 3.333333
# $res2
# 2 4
# 7.5 4.5