次のようにデータを設定しています。
date ID weight
Apr 4 1 21
Apr 5 1 22
Apr 6 1 23
Apr 4 2 30
Apr 5 2 31
Apr 6 2 32
Apr 7 2 12
最後に記録された重量がその ID の最大値ではないケースを調べてみたいと思います。したがって、上記の例では、最後の行がその ID の最大の日付ですID=2
が、その ID の最大の重みではありません。
基本的に、最大日付の重みと ID 内の最大重みを持つデータ フレームを吐き出す for ループを設定でき、差分スコアを実行できます。差スコアが 0 より大きい人は、最後の日付が削除された行が必要です。
subs <- levels(as.factor(df$ID))
newdf <- as.data.frame(rep(subs, each = 1))
names(newdf) <- c('ID')
newdf$max <- NA
newdf$last <- NA
for (i in subs){
subdata = subset(df, ID == i)
lastweight <- subdata$Weight[length(subdata$ID)]
maxweight <- max(subdata$Weight)
newdf$max[IDdate$ID == i]<-maxweight
newdf$last[IDdate$ID == i]<-lastweight
}
IDdate$diff <- as.numeric(IDdate$max) - as.numeric(IDdate$last)
diff>0
今、私が苦労しているのは、ID をどこから取得して元のデータ フレームに移動し、それらの ID の最後の日付を削除できるようにするソリューションを考え出すことです。
私は試しwhich
ましsubset
たが、これは私が望むものではありません。