I have a dataframe that contains 3 columns and I want to create 4th column based on values given in other columns . For creating new_rank column , we start with 1 as a starting point for all users and increase the succeeding rank value by 1 when matric_1 is more than 15 and matric_2 is more than 20..
I feel I need to use cumsum function in r , but am struggling with ifelse conditions . Code for dataframe is given below
df<-data.frame(user_id=c("a","a","a","a","b","b","b","c","c","c","c","c","d","d","d","d"),matric_1=c(10,23,4,5,17,5,40,1,2,18,19,5,18,2,19,2),matric_2=c(10,25,10,13,21,10,7,3,4,22,21,4,23,4,21,4),new_rank=c(1,1,2,2,1,2,2,1,1,1,2,3,1,2,2,3))
User_id matric_1 matric_2 new_rank a 10% 10% 1 a 23% 25% 1 a 4% 10% 2 a 5% 13% 2 b 17% 21% 1 b 5% 10% 2 b 40% 7% 2 c 1% 3% 1 c 2% 4% 1 c 18% 22% 1 c 19% 21% 2 c 5% 6% 3 d 18% 23% 1 d 2% 4% 2 d 19% 21% 2 d 2% 4% 3
Aucun commentaire:
Enregistrer un commentaire