我有类似:[R比较当前行的下一行(在同一列)
ISBN Date Quantity
3457 2004 10
3457 2004 6
3457 2004 10
3457 2005 7
3457 2005 12
9885 2013 10
9885 2013 6
9855 2013 10
9885 2014 7
9885 2014 12
而且我想:
ISBN Date Quantity Year
3457 2004 10 1st Year
3457 2004 6 1st Year
3457 2004 10 1st Year
3457 2005 7 2nd Year
3457 2005 12 2nd Year
9885 2013 10 1st Year
9885 2013 6 1st Year
9855 2013 10 1st Year
9885 2014 7 2nd Year
9885 2014 12 2nd Year
而且我有这样的代码:
df<-df %>% group_by(ISBN) %>% mutate(Year = ifelse(DateYear > DateYear,"1st Year","2nd Year"))
但是我到处都是“第二年”,所以我想在ifelse
的比较中我实际上并没有比较“日期”列中的行。我想我必须使用for循环,但是认为这是在R中的其他方式。我怎样才能得到我需要的结果?
我不明白你的标准是'第一年'和'第二年'?为什么2013年回到第一年,2014年回到第二年? – Phil
@Phil请看这里:https://stackoverflow.com/questions/40159194/r-plot-months-for-the-first-2-years – adlisval