I have a problem with ifelse(), I do not understand how it works.> X<-c(2,2,1,1,0,0) > str(X)num [1:6] 2 2 1 1 0 0> Y<-ifelse(X>0,1,0) > Y[1] 1 1 1 1 0 0>Can some one explain what is going on, I do not understand what ifelse is doing in this case. Can someone explain the output Y. Thanks -- View this message in context: http://www.nabble.com/ifelse%28%29-tp21943308p21943308.html Sent from the R help mailing list archive at Nabble.com.
I have a problem with ifelse(), I do not understand how it works.> X<-c(2,2,1,1,0,0) > str(X)num [1:6] 2 2 1 1 0 0> Y<-ifelse(X>0,1,0) > Y[1] 1 1 1 1 0 0>Can some one explain what is going on, I do not understand what ifelse is doing in this case. Can someone explain the output Y. Thanks -- View this message in context: http://www.nabble.com/ifelse%28%29-tp21943309p21943309.html Sent from the R help mailing list archive at Nabble.com.
Did you read the helpfile? If your condition is true, the first option is returned. If it is false, the second option is returned. For the first four elements of X, all of which are greater than zero, 1 is returned. For the last two, which are not greater than zero, 0 is returned. Sarah On Tue, Feb 10, 2009 at 4:44 PM, kayj <kjaja27 at yahoo.com> wrote:> > I have a problem with ifelse(), I do not understand how it works. > >> X<-c(2,2,1,1,0,0) >> str(X) > num [1:6] 2 2 1 1 0 0 >> Y<-ifelse(X>0,1,0) >> Y > [1] 1 1 1 1 0 0 >> > > Can some one explain what is going on, I do not understand what ifelse is > doing in this case. Can someone explain the output Y. > > Thanks >-- Sarah Goslee http://www.functionaldiversity.org
kayj asks:> I have a problem with ifelse(), I do not understand how it works. > > > X<-c(2,2,1,1,0,0) > > str(X) > num [1:6] 2 2 1 1 0 0 > > Y<-ifelse(X>0,1,0) > > Y > [1] 1 1 1 1 0 0Since X is a vector, the operation X>0 is also a vector. The function ifelse() is correctly providing output for each element of the operation X>0. cur -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.curt@epa.gov 541/754-4638 [[alternative HTML version deleted]]
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of kayj > Sent: Tuesday, February 10, 2009 1:44 PM > To: r-help at r-project.org > Subject: [R] ifelse() > > > I have a problem with ifelse(), I do not understand how it works. > > > X<-c(2,2,1,1,0,0) > > str(X) > num [1:6] 2 2 1 1 0 0 > > Y<-ifelse(X>0,1,0) > > Y > [1] 1 1 1 1 0 0 > > > > Can some one explain what is going on, I do not understand > what ifelse is > doing in this case. Can someone explain the output Y. > > Thanks >ifelse goes through your vector X, 1 element at a time, and if the element is greater than zero it returns a 1, otherwise, 0. The resulting vector of 1s and 0s is assigned to Y Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204
Take it in a couple of steps. 'ifelse' will take the evaluation of a logical vector (first parameter) and return it second parameter if TRUE or the third parameter if FALSE:> X<-c(2,2,1,1,0,0) > X > 0[1] TRUE TRUE TRUE TRUE FALSE FALSE> ifelse(X > 0, 1,0)[1] 1 1 1 1 0 0>Now the second and third parameters can also be vectors and the corresponding value will be chosen:> ifelse(X > 0, 1:6, 11:16)[1] 1 2 3 4 15 16>HTH On Tue, Feb 10, 2009 at 4:44 PM, kayj <kjaja27 at yahoo.com> wrote:> > I have a problem with ifelse(), I do not understand how it works. > >> X<-c(2,2,1,1,0,0) >> str(X) > num [1:6] 2 2 1 1 0 0 >> Y<-ifelse(X>0,1,0) >> Y > [1] 1 1 1 1 0 0 >> > > Can some one explain what is going on, I do not understand what ifelse is > doing in this case. Can someone explain the output Y. > > Thanks > > -- > View this message in context: http://www.nabble.com/ifelse%28%29-tp21943308p21943308.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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 Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Hello, ifelse checks condition whether X (or, ina fact every value in X) is greater than 0. If it is 1 is assigned and if it isn't then 0. 2 and 1 are greater than 0, therefore the first four values of Y are 1. 0 isn't greater than 0, therefore the last two values of Y are 0. 2009/2/10 kayj <kjaja27@yahoo.com>> > I have a problem with ifelse(), I do not understand how it works. > > > X<-c(2,2,1,1,0,0) > > str(X) > num [1:6] 2 2 1 1 0 0 > > Y<-ifelse(X>0,1,0) > > Y > [1] 1 1 1 1 0 0 > > > > Can some one explain what is going on, I do not understand what ifelse is > doing in this case. Can someone explain the output Y. > > Thanks > > -- > View this message in context: > http://www.nabble.com/ifelse%28%29-tp21943308p21943308.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Tue, Feb 10, 2009 at 01:44:17PM -0800, kayj wrote:> > I have a problem with ifelse(), I do not understand how it works. > > > X<-c(2,2,1,1,0,0) > > str(X) > num [1:6] 2 2 1 1 0 0 > > Y<-ifelse(X>0,1,0) > > Y > [1] 1 1 1 1 0 0 > > > > Can some one explain what is going on, I do not understand what ifelse is > doing in this case. Can someone explain the output Y.ifelse evaluates the condition given in its first argument and returns the value of the second argument for all cases found to be TRUE and the value of the third argument otherwise. As the first 4 elments of X fulfill your condition (X>0) the corresponding result for them is 1 and the rest 0. See ?ifelse for details. cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan 85350 Freising, Germany http://mips.gsf.de/staff/pagel