Using the built-in fivenum
, you can obtain:
tapply(x,x>0,fivenum)
Question
I ran this:
GroupSummary <- function(x){
for (i in x) {
if(i>0){
p <-c(summary(x))
r <- c(p)
} else {if(i<0){
n <-c(summary(x))
r <- c(n)
} else {stop}
}
return(r)
}
}
x <- c(-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,1,2,3,4,5,6,7,8,9,10)
GroupSummary(x)
i end up getting this as a result:
Min. 1st Qu. Median Mean 3rd Qu. Max.
-10.00 -5.25 0.00 0.00 5.25 10.00
I am trying to seperate it in two groups one group for positive numbers and another for negative numbers not combine both.Where did i go wrong in the coding i wrote?? Any hints or help are welcome thank you
Solution
Using the built-in fivenum
, you can obtain:
tapply(x,x>0,fivenum)
OTHER TIPS
May advocate for aggregate
?
> x <- c(-(1:10),1:10)
> aggregate(x, by=list(positive=x>0), summary)
positive x.Min. x.1st Qu. x.Median x.Mean x.3rd Qu. x.Max.
1 FALSE -10.00 -7.75 -5.50 -5.50 -3.25 -1.00
2 TRUE 1.00 3.25 5.50 5.50 7.75 10.00
> aggregate(x, by=list(positive=x>0), fivenum)
positive x.1 x.2 x.3 x.4 x.5
1 FALSE -10.0 -8.0 -5.5 -3.0 -1.0
2 TRUE 1.0 3.0 5.5 8.0 10.0
This can be the required function. It works by default for negative/positive, but you can use any index (the default ind=NULL create the positive/negative index). The vectors x
and ind
must have the same length, so we stop the execution if this condition doesn't hold (using stop
).
groupSummary = function(x, ind=NULL) {
if(is.null(ind)) {
ind = character(length(x))
ind[x>=0] = "positive"
ind[x<0] = "negative"
}
if(length(x)!=length(ind)) stop("'x' and 'ind' must have the same length.")
out = do.call(rbind, tapply(x,INDEX=ind,FUN=summary))
return(out)
}
groupSummary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
negative -10 -7.75 -5.5 -5.5 -3.25 -1
positive 1 3.25 5.5 5.5 7.75 10
set.seed(123) # to get the same output for 'colors' index
colors = sample(c("red", "blue", "green"), length(x), replace=TRUE)
groupSummary(x, colors)
Min. 1st Qu. Median Mean 3rd Qu. Max.
blue -9 -5.00 -1 -3.0000 0.0 1
green -10 -6.50 -4 -0.9091 5.0 10
red -3 -0.75 4 3.1670 6.5 9
groupSummary(x, ind=1:3)
Error in groupSummary(x, ind = 1:3) :
'x' and 'ind' must have the same length.