So the thing is this, ifelse puts together a vector elementwise from two
other vectors (or other things, but vectors is vague enough for now) based
on a series of T/F values: you only put in one logical test, so you only get
a vector of length 1 back, here MODE1[1]=4. It seems like a regular if/else
statement (not the vector function) would do the job here:

if (sum(df$Freq)/length(df$Freq)==1){
    warning("No Mode: Frequency of all values is 1", call. = FALSE)
} else {
    print(MODE1)
}

If for whatever reason you really don't want to use the if/else structure, I
suppose you could repeat your logical test three times e.g.,

test <- sum(df$Freq)/length(df$Freq)==1; test = rep(test,length(MODE1));
ifelse(test,warning("No Mode: Frequency of all values is 1", call. =
FALSE),MODE1)

but don't do that: it's ridiculous.

Hope this works for you,

Michael Weylandt


On Sat, Jul 30, 2011 at 12:48 AM, Tyler Rinker <tyler_rin...@hotmail.com>wrote:

>
> Greetings R Community,
>
> I am working with the ifelse function and it is returning something
> unexpected.  In the code the line with the MODE1 assignment the output is a
> vector [1] 4 5 6  but when I put the MODE1 object into the ifelse function
> [R}'s output for MODE1 is the first number from the string (4).  Why is
> this?  Given the supplied vector of x I would assume both the MODE1 and
> ifelse() lines to return the same result.  I would like the ifelse to return
> the entire vector [1] 4 5 6 as in the previous line.
>
> OS: Win7
> R version 2.14 beta
>
>
> #=======================================================================
> #                                        Beginning of code
> #=======================================================================
> x<-c(2,3,4,4,5,5,6,6,8,10)
>
> df<-as.data.frame(table(x))
> df<-df[order(df$Freq),]
> m<-max(df$Freq)
> (MODE1<-as.vector(as.numeric(as.character(subset(df,Freq==m)[,1]))))
> ifelse(sum(df$Freq)/length(df$Freq)==1,warning("No Mode: Frequency of all
> values is 1", call. = FALSE),MODE1)
> #=======================================================================
> #                                             End of code
> #=======================================================================
>
>                                                R Console Output
> > (MODE1<-as.vector(as.numeric(as.character(subset(df,Freq==m)[,1]))))
> [1] 4 5 6
> > ifelse(sum(df$Freq)/length(df$Freq)==1,warning("No Mode: Frequency of all
> values is 1", call. = FALSE),MODE1)
> [1] 4
>
> Thank you in advance,
> Tyler
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to