ID DAYS FREQUENCY
"ads" 20 3
"jwa" 45 2
"mno" 4 1
"ads" 13 3
"jwa" 60 2
"ads" 18 3
數據表我想補充一點,減去根據id的日子一列,減去最接近在一起的日子。 我的新表想是這樣的:
ID DAYS FREQUENCY DAYS DIFF
"ads" 20 3 2 (because 20-18)
"jwa" 45 2 NA (because no value greater than 45 for that id)
"mno" 4 1 NA
"ads" 13 3 NA
"jwa" 60 2 15
"ads" 18 3 5
獎勵:有沒有使用合併功能的方法嗎?
爲什麼你想/希望在這裏使用合併? Fwiw,如果你願意安裝一個軟件包,可以使用'library(data.table); setDT(DF)[order(DAYS),dd:= DAYS - shift(DAYS),by = ID]' – Frank