2
下面是一些R代碼裏面有1天的滯後與自身合併富:自我合併數據幀與R中的滯後?
foo <- data.frame(user=c(10,10,10,11,11,11),
day=c(1,2,3,1,2,3),
something=c('a', 'b', 'c', 'd', 'e', 'f'))
foo$prev_day <- foo$day - 1
foo2 <- merge(foo, foo,
by.x=c('user', 'day'),
by.y=c('user', 'prev_day'))
#Warning message:
#In merge.data.frame(foo, foo, by.x = c("user", "day"), by.y = c("user", :
# column name ‘day’ is duplicated in the result
foo2
user day something.x prev_day day something.y
1 10 1 a 0 2 b
2 10 2 b 1 3 c
3 11 1 d 0 2 e
4 11 2 e 1 3 f
注意,它抱怨並已在結果「天」的兩倍,但在其他方面看起來還不錯(每個用戶只合並本身)。
什麼是最簡單的方法來做到這一點,即沒有警告,只有第一天'列',而不是第二個結果?
把這個作爲答案,我會接受它! – dfrankow