Bryan Adams
Bryan Adams

Reputation: 174

mutate based of the value of multiple columns

Is there an easier way to create a binary column by referencing multiple columns then this?

 mutate(S.325=ifelse(PR1==325|PR2==325|PR3==325|PR4==325|
                        PR5==325|PR6==325|PR7==325|PR8==325|
                        PR9==325|PR10==325|PR11==325|PR12==325|PR13==325|
                        PR14==325|PR15==325,1,0))

Upvotes: 1

Views: 39

Answers (1)

akrun
akrun

Reputation: 886938

We can use rowSums

df1 %>% 
    mutate(S.325 = as.integer(rowSums(.[paste0("PR", 1:15)] == 325) >0))

Upvotes: 1

Related Questions