I have a dataframe with df
with two columns called item
and week
. This is how it looks like:
week item
1179 63230
1179 63233
1180 63230
1180 63233
1181 63230
1181 63233
I would like to find the first and last week
of occurrence of each item
in the dataframe, and then compute the difference between last and end week
. I tried to use the solution provided in this link - How can I find the first and last occurrences of an element in a data.frame?. The code that I used is as follows:
df_start <- df[!duplicated(df$item),]#Get starting week
df_end <- df[rev(!duplicated(rev(df$ITEM))),]#Get ending week
But, this does not give me the correct start and end week
. Could someone help me out?
I am also including an expected output. df
has about 3 million rows, but the output for the above portion of df
should be as follows:
>df_start
item start_week
63230 1179
63233 1179
>df_end
item end_week
63230 1181
63233 1181