r - dplyr mutate refer new column itself

后端 未结 2 626
滥情空心
滥情空心 2021-01-26 23:42

I have a data frame like this named \'a\'.

   ID        V1
   1         -1 
   1          0 
   1          1 
   1        1000 
   1          0 
   1          1
         


        
相关标签:
2条回答
  • 2021-01-27 00:06

    We can try

    a %>%
         group_by(ID) %>% 
         mutate(V2 = cumsum(V1 >= 1000)+1L)
    #     ID    V1    V2
    #  <int> <int> <int>
    #1     1    -1     1
    #2     1     0     1
    #3     1     1     1
    #4     1  1000     2
    #5     1     0     2
    #6     1     1     2
    #7     2    -1     1
    #8     2     0     1
    #9     2  1000     2
    

    data

    a <- structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), 
    V1 = c(-1L, 
    0L, 1L, 1000L, 0L, 1L, -1L, 0L, 1000L)), .Names = c("ID", "V1"
    ), class = "data.frame", row.names = c(NA, -9L))
    
    0 讨论(0)
  • 2021-01-27 00:17

    This should work:

    a %>% group_by(ID) %>% mutate(V2 = ifelse(row_number() == 1, 1, 0) + 
                                    ifelse(row_number() > 1 & V1 <= 1000, 1, 0) + 
                                    cumsum(ifelse(V1 >= 1000, 1, 0)))
    

    Update: Changed second ifelse logic statement from row_number() > 1 & V1 < 1000 to that shown above. This alteration should give the results as requested in the comments.

    0 讨论(0)
提交回复
热议问题