[R] how to create a new column from two columns with conditions

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Apr 29 23:10:47 CEST 2020


Hello,

Here is another way. The condition returns FALSE/TRUE or 0/1. Add 1 to 
get the expected result.
It has the advantage of being faster.

b$PHENO <- (b$FLASER == 2 | b$PLASER == 2) + 1L


Hope this helps,

Rui Barradas

Às 20:42 de 29/04/20, Ana Marija escreveu:
> Thanks, I did this:
> b$PHENO<- ifelse(b$FLASER ==2 | b$PLASER ==2, 2, 1)
> 
> On Wed, Apr 29, 2020 at 2:36 PM Ivan Krylov <krylov.r00t using gmail.com> wrote:
>>
>> On Wed, 29 Apr 2020 14:19:18 -0500
>> Ana Marija <sokovic.anamarija using gmail.com> wrote:
>>
>>> My conditions for creating a new column PHENO would be this:
>>>
>>> if FLASER or PLASER =2 then PHENO=2
>>> otherwise PHENO=1
>>
>> On Wed, 29 Apr 2020 15:30:45 -0400
>> "Patrick (Malone Quantitative)" <malone using malonequantitative.com> wrote:
>>
>>> If you don't mind using tidyverse, you can do this easily with
>>> if_else.
>>
>> ...and if you want to stay with base R, you can use the ifelse
>> function.
>>
>> --
>> Best regards,
>> Ivan
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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