You might want to breakdown some of the expressions you are using; for example:

> 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]))))
[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
>
> sum(df$Freq)/length(df$Freq)==1
[1] FALSE
>  sum(df$Freq)/length(df$Freq)
[1] 1.428571
>

notice that 'sum(df$Freq)/length(df$Freq)' only returns a single value
and the expression in the 'ifelse' evaluates to FALSE, so only the
first value of MODE1 is returned.

So when you get unexpected results like this, you need to breakdown
your expressions to see what is really be evaluated.

On Sat, Jul 30, 2011 at 1: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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
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