I am trying to find the first/last observation by group. I tired both R and excel (because it is so slow in R so I tried excel). The excel took less than one second, but R took 8 MINUTES!!!. The code logic for both are almost the same.
The data is a panel data regarding purchasing fruit. The same shopper could buy multiple times at different time. I have 233,000 observations. Data is like(sorted by day shopper first and day):
Day Shopper Choice
1 A apple
2 A apple
1 B Banana
1 C apple
2 C Banana
3 C apple
1 D berry
2 D berry
My r code for the first obseravtion. I want to tag the first observation in a group by indicator "1", a new column.
for (i in 1:n)
{ ifelse (dt$shopper[i+1]==dt$shopper[i],newcol[i+1]<-0,newcol[i+1]<-1)
}
My excel code is: if(B2<>B1,1,0)
I need the repurchase matrix given the same shopper. To define the repurchase : the repurchase of the "1st purchase" is the "2nd purchase"; And the repurchase of the "2nd purchase" is the "3rd purchase" The last purchase has no repurchase. Sorry it sounds like a twister. so my solution is get two choice col and move the second col one row above so I can calculate the repurchase matrix by shopper/or aggregate.The desired output for tagging first obs by group should be as follow. With the columns of choice and choice 2 and I can calculate the repurchase matrix by nrow.
Day Shopper Choice tagging choice 2
1 A apple 0 *apple*
2 A apple *apple* 0
1 B Banana 0 0
1 C apple 0 Banana
2 C Banana Banana apple
3 C apple apple 0
1 D berry 0 *berry*
2 D berry *berry* 0
[update]. If there is only one purchase for this user, there is no repurchase. If the purchase is the last purchase of the user, there is no repurchase. So the final repurchase matrix by choice in this case is
second inside bracket are the probability
first apple banana berry
apple 1 (0.5) 1 0
banana 1 0 (0) 0
berry 0 0 1 (1)
Below is how I calculate the repurchase matrix after tagging the first purchase of the user. i is the row (apple, banana, Berry etc) and j (apple, banana, Berry etc) is the column. [the speed is ok give the fact that I repurchase matrix is 40*40 after tagging and adding the seconding choice column)
for (i in 1:n){
for(j in 1:n){
repurchase_matrix[i,j]=nrow(dt[dt[,1]==i&dt[,2]==j,])}}
See Question&Answers more detail:os