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]]
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_rinker at 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 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve?
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_rinker@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]]
Thanks toy Jim and Michael for their response. The suggestion to use regular if/else was spot on.> From: tyler_rinker@hotmail.com > To: r-help@r-project.org > Date: Sat, 30 Jul 2011 01:48:21 -0400 > Subject: [R] ifelse returns > > > 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]]