dimanche 19 août 2018

How to mutate a new column with yes or no value if at least one column from col7 to col10 is equal to 4 in r using dplyr?

I have a data as follows:

 col1 <- c(0.1,0.2,0.0,0.5,0.6)
 col2 <- c(2,2,4,5,6)
 col3 <- c(1,4,3,4,5) 
 col4 <- c(2,3,4,4,6)
 col5 <- c(5,3,3,2,1)
 data.frame(col1,col2,col3,col4,col5)

   col1 col2 col3 col4 col5
 1  0.1    2    1    2    5
 2  0.2    2    4    3    3
 3  0.0    4    3    4    3
 4  0.5    5    4    4    2
 5  0.6    6    5    6    1

I would like to add a new column with "yes" value where in each row at least one column from col2 to column 5 is equal to 4 and "no" when the data does not meet the criteria.

So the output would look like as:

   col1 col2 col3 col4 col5 col6
 1  0.1    2    1    2    5  no
 2  0.2    2    4    3    3  yes
 3  0.0    4    3    4    3  yes
 4  0.5    5    4    4    2  yes
 5  0.6    6    5    6    1  no

here is my command:

new.df <- df %>% mutate(df, col6 = funs(ifelse(abs(vars(c(2:5) == 4),"yes", "no")

But I can not get the required output. do you have any idea how can I use dplyr, mutate and if else function to get the result?

Aucun commentaire:

Enregistrer un commentaire