简体   繁体   English

R 中的函数:如何在同一 Function 内返回均值、中值、标准差

[英]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) 
}

Usage:用法:

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.

相关问题 如何通过在 R 中创建额外的列(均值和标准差)来获得相同数据帧的均值和标准差的结果 - How to get the results of a mean and standard deviation to the same data frame by creating extra columns (mean and standard deviation) in R 如何计算R中多个标准差的平均值 - How to calculate mean of multiple standard deviation in R 如何生成均值,标准偏差相同但R中形状或分布完全不同的数据? - How do I generate data that have the same mean, standard deviation but very different shapes or distributions in R? 从单个 rnorm 函数调用返回均值和标准差 - Return the mean and standard deviation from a single rnorm function call 如何找到与 R 代码中的平均值相差一个标准偏差内的数据点(在一列内)的比例? - How do I find proportion of the datapoints (within one column) that are within one standard deviation away from the mean in R code? 如何在R中按组同时计算均值,cv和标准差 - How to compute mean, cv and standard deviation simultaneously using by group in R 如何计算 R 中每个 ID 的观察值? (并获得平均值和标准差) - How to count observations per ID in R? (and obtain mean and standard deviation) 如何确定R中同一列中多个类别的标准偏差 - How do I determine the standard deviation of multiple categories within the same column in R 如何使用R中的均值和标准偏差替换缺失值? - How to replace missing values using Mean and Standard Deviation in R? 如何为 r 中的指数分布生成估计参数(标准差和均值)? - How to generate the estimate parameters (Standard deviation and mean) for an exponential distribution in r?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM