Reputation: 99
How can I return the mean, median, and standard deviation within same function in R? All that I can get to return is the last part of the function which calculated the standard deviation. I was thinking that by assigning summarystat(Tail_wags) to b that when I returned 'b' that I would have all three value. Added the result for the three values I need outside of the function after 'b' to see what values are supposed to be.
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
Upvotes: 3
Views: 4534
Reputation: 1816
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. Per default, the function will omit NA's and calculate the mean, median and sd.
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.
The mean()
function, for example, will return NA when calculated for a vector that includes 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.
Upvotes: 0
Reputation: 552
Alternatively, you can get rid of the function completely and use something like pastecs::stat.desc
and then subtract the values you want
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.
Upvotes: 1
Reputation: 3791
You can combine and return the variables using the generic 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
Upvotes: 3
Reputation: 5788
Function and application:
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))
Upvotes: 1
Reputation: 3450
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
Upvotes: 1
Reputation: 6073
You can only return one object from a function. 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))
}
Upvotes: 3