#r #dataframe
#r #фрейм данных
Вопрос:
Как создать новый столбец, чтобы a.) когда значение userID
достигнет единицы, следующая строка изменит ID
или b.) следующая строка изменит userID.
Например:
df_base lt;- structure(list( userid = c("5465d","5465d","5465d","5465d","6765b","6765b","7995b","7995b","7988b","7778b"), sells = c(1, 0, 0, 1,0,0,0,1,0,0)), .Names=c("userid", "sells"), row.names = c("1", "2", "3", "4","5","6","7","8","9","10"), class =("data.frame"))
После создания столбца
df_base_result lt;- structure(list( userid = c("5465d","5465d","5465d","5465d","6765b","6765b","7995b","7995b","7988b","7778b"), sells = c(1, 0, 0, 1,0,0,0,1,0,0), ids = c("ID1","ID2","ID2","ID2","ID3","ID3","ID4","ID4","ID5","ID6")), .Names=c("userid", "sells","ids"), row.names = c("1", "2", "3", "4","5","6","7","8","9","10"), class =("data.frame"))
Ответ №1:
df_base %gt;% group_by(userid) %gt;% mutate(a = cumsum(lag(sells,default = 0)))%gt;% ungroup() %gt;% mutate(ids = paste0('ID',data.table::rleid(userid, a))) %gt;% select(-a) # A tibble: 10 x 3 userid sells ids lt;chrgt; lt;dblgt; lt;chrgt; 1 5465d 1 ID1 2 5465d 0 ID2 3 5465d 0 ID2 4 5465d 1 ID2 5 6765b 0 ID3 6 6765b 0 ID3 7 7995b 0 ID4 8 7995b 1 ID4 9 7988b 0 ID5 10 7778b 0 ID6
Ответ №2:
Вы можете попробовать
x lt;- 1 res lt;- c(x) for (i in 2:nrow(df_base)){ if(df_base$userid[i] != df_base$userid[i-1] | (df_base$sells[i] == 0 amp;amp; df_base$sells[i-1] ==1)){ x lt;- x 1 res lt;- c(res, x) } else { x lt;- x res lt;- c(res, x) } } df_base$ids lt;- paste0("ID",res) df_base userid sells ids 1 5465d 1 ID1 2 5465d 0 ID2 3 5465d 0 ID2 4 5465d 1 ID2 5 6765b 0 ID3 6 6765b 0 ID3 7 7995b 0 ID4 8 7995b 1 ID4 9 7988b 0 ID5 10 7778b 0 ID6