Hi,
Why this function doesn't work?
function (x)
{
if (is.factor(x)) {
if (!is.ordered(x)) {
warning("La mediana non si puo' calcolare!!!")
return(NA)
}
me <- median(unclass(x))
if (me - floor(me) != 0) {
warning("Mediana indeterminata")
return(NA)
}
else {
levels(x)[me]
}
}
else if(class(x)=="histogram"){
N<-sum(x$counts)
cl<-min(which(cumsum(x$counts)>=N/2))
return(x$breaks[cl]+
(N/2-sum(x$counts[1:(cl-1)]))/(x$densit[cl]*N)
} else median(x)
}
thanks
Angelo Scozzarella
Please give commented, minimal, self-contained, reproducible code. In this case, not only the function definition, but a simple example showing *how* it does not work, and what you were expecting it to do! Angelo Scozzarella wrote:> Hi, > > Why this function doesn't work? > > function (x) > { > if (is.factor(x)) { > if (!is.ordered(x)) { > warning("La mediana non si puo' calcolare!!!") > return(NA) > } > me <- median(unclass(x)) > if (me - floor(me) != 0) { > warning("Mediana indeterminata") > return(NA) > } > else { > levels(x)[me] > } > } > else if(class(x)=="histogram"){ > N<-sum(x$counts) > cl<-min(which(cumsum(x$counts)>=N/2)) > return(x$breaks[cl]+ > (N/2-sum(x$counts[1:(cl-1)]))/(x$densit[cl]*N) > } else median(x) > } > > > > thanks > > > Angelo Scozzarella > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
At a first guess, because you are missing a right paren in thereturn() statement of the final else section. Otherwise, we'd need to know about what "doesn't work" means - what is x supposed to be? What do you want the function to do? What error messages are you getting? You haven't provided any of the information we need to actually help you. The posting guide describes what you should include, and is linked at the end of every message. Sarah On Tue, Jul 22, 2008 at 2:51 PM, Angelo Scozzarella <angeloscozzarella at tiscali.it> wrote:> Hi, > > Why this function doesn't work? > > function (x) > { > if (is.factor(x)) { > if (!is.ordered(x)) { > warning("La mediana non si puo' calcolare!!!") > return(NA) > } > me <- median(unclass(x)) > if (me - floor(me) != 0) { > warning("Mediana indeterminata") > return(NA) > } > else { > levels(x)[me] > } > } > else if(class(x)=="histogram"){ > N<-sum(x$counts) > cl<-min(which(cumsum(x$counts)>=N/2)) > return(x$breaks[cl]+ > (N/2-sum(x$counts[1:(cl-1)]))/(x$densit[cl]*N) > } else median(x) > } > > > > thanks > > > Angelo Scozzarella >-- Sarah Goslee http://www.functionaldiversity.org
2008/7/22 Angelo Scozzarella <angeloscozzarella at tiscali.it>:> Hi, > > Why this function doesn't work?> return(x$breaks[cl]+ > (N/2-sum(x$counts[1:(cl-1)]))/(x$densit[cl]*N) > } else median(x) > }Have you read the error message? I get: Error: unexpected '}' in: ":(cl-1)]))/(x$densit[cl]*N) }" which is because you haven't closed the "(" from the "return(" function. If you use an editor with syntax highlighting (emacs+ESS, tinn-R, others) it will flash matching brackets and parentheses so you don't make this kind of mistake. Barry
Hi Angelo,
please read the posting guide, before posting!
Anyway, for general rule, if you want to
see what's going on in a function at each step, it may be helpful
to install library("debug")
that has very useful functions as "mtrace" that
my resolve your problem.
Cheers
Anna
----- Messaggio originale -----
Da: Angelo Scozzarella <angeloscozzarella@tiscali.it>
A: r-help@r-project.org
Inviato: Martedì 22 luglio 2008, 20:51:05
Oggetto: [R] Function Error
Hi,
Why this function doesn't work?
function (x)
{
if (is.factor(x)) {
if (!is.ordered(x)) {
warning("La mediana non si puo' calcolare!!!")
return(NA)
}
me <- median(unclass(x))
if (me - floor(me) != 0) {
warning("Mediana indeterminata")
return(NA)
}
else {
levels(x)[me]
}
}
else if(class(x)=="histogram"){
N<-sum(x$counts)
cl<-min(which(cumsum(x$counts)>=N/2))
return(x$breaks[cl]+
(N/2-sum(x$counts[1:(cl-1)]))/(x$densit[cl]*N)
} else median(x)
}
thanks
Angelo Scozzarella
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Anna Freni Sterrantino
Ph.D Student
Depatment of Statistics
University of Bologna, Italy
via Belle arti 41, 40124 Bo
Posta, news, sport, oroscopo: tutto in una sola pagina.
[[elided Yahoo spam]]
www.yahoo.it/latuapagina
[[alternative HTML version deleted]]