Hi, My data frame looks like A B C a b a x y z I want to add a new column which says "y" if either A or B matches with C and "n" if there is no match. How can I do that? Thanks [[alternative HTML version deleted]]
Suppose your data is called dfs: dfs$NEW_COLUMN <- ifelse(dfs$A == dfs$C | dfs$B == dfs$C, "y", "n") which says if dfs column A equals dfs column C or dfs column B equals dfs column C, put "y" else put "n"; then put the resulting vector into NEW_COLUMN of dfs. Note that if your data are factors, this won't work and you'll need to wrap most things with as.character() For more, see the manual "An Introduction to R" available to you by typing help.start() at the interactive prompt. Cheers, Michael On Tue, Aug 28, 2012 at 7:06 PM, Sapana Lohani <lohani.sapana at ymail.com> wrote:> Hi, My data frame looks like > > A B C > > a b a > > x y z > > > I want to add a new column which says "y" if either A or B matches with C and "n" if there is no match. How can I do that? > > Thanks > > [[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.
HI, If I understand your question correctly, this should give the result: dat1<-read.table(text=" A B C a b a x y z ",sep="",header=TRUE,stringsAsFactors=FALSE) within(dat1,{new_column<-ifelse(A==C|B==C,"y","n")}) ?# A B C new_column #1 a b a????????? y #2 x y z????????? n A.K. ----- Original Message ----- From: Sapana Lohani <lohani.sapana at ymail.com> To: R help <r-help at r-project.org> Cc: Sent: Tuesday, August 28, 2012 8:06 PM Subject: [R] Use or ?? Hi, My data frame looks like A B C a b a x y z I want to add a new column which says "y" if either A or B matches with C and "n" if there is no match. How can I do that? Thanks ??? [[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.
Have a look at the documentation for ifelse() http://stat.ethz.ch/R-manual/R-devel/library/base/html/ifelse.html that should do what you need. Rhydwyn -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Sapana Lohani Sent: Wednesday, August 29, 2012 10:06 AM To: R help Subject: [R] Use or ?? Hi, My data frame looks like A B C a b a x y z I want to add a new column which says "y" if either A or B matches with C and "n" if there is no match. How can I do that? Thanks [[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. ########################################################################################################################################################################################################################### This email and any files transmitted with it are intende...{{dropped:16}}
On Aug 28, 2012, at 9:54 PM, arun wrote:> HI, > If I understand your question correctly, this should give the result: > dat1<-read.table(text=" > A B C > a b a > x y z > ",sep="",header=TRUE,stringsAsFactors=FALSE) > within(dat1,{new_column<-ifelse(A==C|B==C,"y","n")}) > # A B C new_column > #1 a b a y > #2 x y z nA difficulty for a newbie is that this might give the impression that the "new_column" actually exists somewhere. If at that point one type "dat1" at the console, you would only get three columns. You need to do this: dat1 <- within(dat1,{new_column<-ifelse(A==C|B==C,"y","n")}) -- David.> ----- Original Message ----- > From: Sapana Lohani <lohani.sapana at ymail.com> > To: R help <r-help at r-project.org> > Cc: > Sent: Tuesday, August 28, 2012 8:06 PM > Subject: [R] Use or ?? > > Hi, My data frame looks like > > A B C > > a b a > > x y z > > > I want to add a new column which says "y" if either A or B matches > with C and "n" if there is no match. How can I do that? > > Thanks > > [[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. > > > ______________________________________________ > 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.David Winsemius, MD Alameda, CA, USA