[R] Use or ??

R. Michael Weylandt michael.weylandt at gmail.com
Wed Aug 29 06:15:42 CEST 2012


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.




More information about the R-help mailing list