如果以前曾詢問過,請道歉。我找不到任何滿意的答案,儘管聽起來應該是一個相當直接的操作。將dplyr過濾器操作應用於特定行而不會丟失其他數據
我有我的數據
transition_frame name state_number lifetime
<int> <chr> <dbl> <dbl>
1 38 //Traces_exp1_tif_pair10 1 NA
2 44 //Traces_exp1_tif_pair10 2 6
3 352 //Traces_exp1_tif_pair10 3 308
4 362 //Traces_exp1_tif_pair10 4 10
5 379 //Traces_exp1_tif_pair10 5 17
6 388 //Traces_exp1_tif_pair10 6 9
這是很容易計算的過渡幀之間的橫行差異,但因爲有狀態0和1之間沒有「轉型」,它打破了流量。
我怎樣才能讓只有第一行是transition_frame - 1
(提示,這是37),不接觸任何其他數據?
試想一下,
group_by(name) %>%
filter(state_number == 1) %>%
mutate(lifetime = transition_frame - 1) %>%
unfilter() # To retrieve dropped data
這將導致整個集,從第一行計算,並且不只的第一行。
transition_frame name state_number lifetime
<int> <chr> <dbl> <dbl>
1 38 //Traces_exp1_tif_pair10 1 37
2 44 //Traces_exp1_tif_pair10 2 6
3 352 //Traces_exp1_tif_pair10 3 308
4 362 //Traces_exp1_tif_pair10 4 10
5 379 //Traces_exp1_tif_pair10 5 17
6 388 //Traces_exp1_tif_pair10 6 9
我真的不明白你的問題,但也許你正在尋找'coalesce'。 – Axeman
增加了一些代碼。這是否解釋得更好? –
請添加您的預期輸出。 – Prradep