[R] Use or ??

David Winsemius dwinsemius at comcast.net
Wed Aug 29 08:42:57 CEST 2012


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          n


A 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




More information about the R-help mailing list