I am writing an R code that selects top 2 movies for each user, among this 10, maximum of 1 are sponsored movies. The data is sorted based on user rating as follow:
user movie rating sponsored
10 m23 3.4 1
2 m5 3.3 0
6 m74 3.3 1
10 m3 3.2 0
6 m2 3.1 0
10 m54 3.0 1
6 m13 2.8 0
2 m74 2.6 1
2 m12 2.5 0
Now since I have to sort based on rating in general, not withing each user, I was wondering how I hold variables like the number of movies within each user(K = 2) and max number of sponsored movies(S = 1) for each user? Should I create different tables for each user with their 2 movies? And if yes, how? The following is basically my algorithm:
n: number of users
m: number of movies
for(i in 1:nm){
if(K_u_i < 2 && S_u_i <= 1)
add that movie to top 2 list of that user
}
Please let me know if any further clarification is needed.
Thank you