How to find if ANY column has a specific value I am looking for?

后端 未结 5 552
我在风中等你
我在风中等你 2021-01-18 20:56
   id first  middle  last       Age
    1 Carol  Jenny   Smith      15
    2 Sarah  Carol   Roberts    20
    3 Josh   David   Richardson 22

I am t

5条回答
  •  有刺的猬
    2021-01-18 21:50

    Another option using mutate and if_else() as you suggested:

    library(tidyverse)
    
    data = read_table("   id first  middle  last       Age
        1 Carol  Jenny   Smith      15
        2 Sarah  Carol   Roberts    20
        3 Josh   David   Richardson 22")
    data %>%
      mutate(carol = if_else(first == "Carol" | middle == "Carol" | last == "Carol",
                             "yes",
                             "no"))
    

    Result:

    # A tibble: 3 x 6
         id first middle last         Age carol
                 
    1     1 Carol Jenny  Smith         15 yes  
    2     2 Sarah Carol  Roberts       20 yes  
    3     3 Josh  David  Richardson    22 no 
    

提交回复
热议问题