[英]Functions in R: How to Return Mean, Median, Standard Deviation Within Same Function
How can I return the mean, median, and standard deviation within same function in R?如何返回 R 中相同 function 内的平均值、中值和标准差? All that I can get to return is the last part of the function which calculated the standard deviation.
我所能返回的只是计算标准偏差的 function 的最后一部分。 I was thinking that by assigning summarystat(Tail_wags) to b that when I returned 'b' that I would have all three value.
我在想,通过将 summarystat(Tail_wags) 分配给 b ,当我返回 'b' 时,我将拥有所有三个值。 Added the result for the three values I need outside of the function after 'b' to see what values are supposed to be.
在“b”之后添加了我在 function 之外需要的三个值的结果,以查看应该是什么值。
Dog_biscuits <- c(0,1,2,3,4,5,6,7,8,9,10)
Tail_wags <- c(0,0,1,3,8,13,14,12,15,16,14)
dog_wags<-cbind(Dog_biscuits,Tail_wags)
dog_wags
summarystat<- function(x) {
z1 <- mean(x)
z2<-median(x)
z3<-sd(x)
}
b<-summarystat(Tail_wags)
b
b
[1] 6.497552
> mean(Tail_wags)
[1] 8.727273
> median(Tail_wags)
[1] 12
> sd(Tail_wags)
[1] 6.497552
You can only return one object from a function.您只能从 function 中返回一个 object。 The trick to achieve what you want is to return a list:
实现你想要的技巧是返回一个列表:
summarystat<- function(x) {
z1 <- mean(x)
z2 <- median(x)
z3 <- sd(x)
return(list(mean=z1, median=z2, sd=z3))
}
You can combine and return the variables using the generic c()
function.您可以使用通用
c()
function 组合并返回变量。
summarystat<- function(x) {
z1 <- mean(x, na.rm = TRUE)
z2<-median(x, na.rm = TRUE)
z3<-sd(x,na.rm = TRUE)
return(c(mean=z1,median=z2,standard_dev=z3))
}
Tail_wags <- c(0,0,1,3,8,13,14,12,15,16,14)
summarystat(Tail_wags)
# mean median standard_dev
# 8.727273 12.000000 6.497552
Your are looking after something like:你正在照顾类似的东西:
summarystat <- function(x) {
my_list <- list("mean" = mean(x), "median" = median(x), "sd" = sd(x))
return(my_list)
}
vals <- summarystat(Tail_wags)
> a$mean
> a$sd
> a$median
Function and application: Function及应用:
do.call("rbind", lapply(dog_wags, function(x){
list(mean_val = mean(x),
median_val = median(x),
sd_val = sd(x))
}
)
)
Data:数据:
Dog_biscuits <- c(0,1,2,3,4,5,6,7,8,9,10)
Tail_wags <- c(0,0,1,3,8,13,14,12,15,16,14)
dog_wags <- data.frame(cbind(Dog_biscuits,Tail_wags))
Alternatively, you can get rid of the function completely and use something like pastecs::stat.desc
and then subtract the values you want或者,您可以完全摆脱 function 并使用类似
pastecs::stat.desc
的东西,然后减去您想要的值
Dog_biscuits <- c(0,1,2,3,4,5,6,7,8,9,10)
Tail_wags <- c(0,0,1,3,8,13,14,12,15,16,14)
dog_wags<-cbind(Dog_biscuits,Tail_wags)
pastecs::stat.desc(Tail_wags)[["mean"]]
# 8.727273
Check out this article for more summary functions.查看这篇文章以获取更多摘要功能。
A somewhat different approach that lets one choose the functions to return.一种稍微不同的方法,可以让人们选择要返回的函数。
Code:代码:
fooapply <- function(x, functions = c("mean", "median", "sd"), na.rm = T){
func <- functions
vec <- c()
for(i in 1:length(func)){
if(na.rm == T){
eval(parse(text = paste0("vec[", i,"]", "<-", func[i], "(x, na.rm = T)")))
}
else{
eval(parse(text = paste0("vec[", i,"]", "<-", func[i], "(x)")))
}
}
names(vec) <- functions
return(vec)
}
Result结果
To obtain your desired result you can just your vector into the function.要获得您想要的结果,您可以将您的向量放入 function。 Per default, the function will omit NA's and calculate the mean, median and sd.
默认情况下,function 将省略 NA 并计算平均值、中位数和标准差。
fooapply(Tail_wags)
mean median sd
8.727273 12.000000 6.497552
Additionally, one can also add or remove functions or swap them out:此外,还可以添加或删除功能或将它们换掉:
fooapply(Tail_wags, c("mean", "median", "IQR"))
Note that some of the included functions will report an error when NA's are included without specifying na.rm = T
, others will just report NA as result.请注意,当包含 NA 而不指定
na.rm = T
时,某些包含的函数会报告错误,其他函数只会报告 NA 作为结果。 The mean()
function, for example, will return NA when calculated for a vector that includes NA.例如,
mean()
function 在计算包含 NA 的向量时将返回 NA。 In contrast, IQR()
will throw an error when NA's are included within the vector and hence requires the na.rm = T
(which is set as TRUE by default) statement in order for fooapply()
to work.相反,当向量中包含 NA 时,
IQR()
将引发错误,因此需要na.rm = T
(默认设置为 TRUE)语句才能使fooapply()
工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.