dplyr mutate with conditional values

后端 未结 3 2139
我寻月下人不归
我寻月下人不归 2020-11-27 10:34

In a large dataframe (\"myfile\") with four columns I have to add a fifth column with values conditionally based on the first four columns.

Prefer answers with

3条回答
  •  甜味超标
    2020-11-27 11:01

    Try this:

    myfile %>% mutate(V5 = (V1 == 1 & V2 != 4) + 2 * (V2 == 4 & V3 != 1))
    

    giving:

      V1 V2 V3 V4 V5
    1  1  2  3  5  1
    2  2  4  4  1  2
    3  1  4  1  1  0
    4  4  5  1  3  0
    5  5  5  5  4  0
    

    or this:

    myfile %>% mutate(V5 = ifelse(V1 == 1 & V2 != 4, 1, ifelse(V2 == 4 & V3 != 1, 2, 0)))
    

    giving:

      V1 V2 V3 V4 V5
    1  1  2  3  5  1
    2  2  4  4  1  2
    3  1  4  1  1  0
    4  4  5  1  3  0
    5  5  5  5  4  0
    

    Note

    Suggest you get a better name for your data frame. myfile makes it seem as if it holds a file name.

    Above used this input:

    myfile <- 
    structure(list(V1 = c(1L, 2L, 1L, 4L, 5L), V2 = c(2L, 4L, 4L, 
    5L, 5L), V3 = c(3L, 4L, 1L, 1L, 5L), V4 = c(5L, 1L, 1L, 3L, 4L
    )), .Names = c("V1", "V2", "V3", "V4"), class = "data.frame", row.names = c("1", 
    "2", "3", "4", "5"))
    

    Update 1 Since originally posted dplyr has changed %.% to %>% so have modified answer accordingly.

    Update 2 dplyr now has case_when which provides another solution:

    myfile %>% 
           mutate(V5 = case_when(V1 == 1 & V2 != 4 ~ 1, 
                                 V2 == 4 & V3 != 1 ~ 2,
                                 TRUE ~ 0))
    

提交回复
热议问题