rank - R: choose different raw and separate to new ranked set -
i have dataset looks like:
interest age gender scored.probabilities al008 18-24 male 0.211 al024 25-34 male 0.022 al008 35-44 female 0.102 al008 25-34 female 0.002 al024 13-17 male 0.102 al035 35-44 female 0.027 al024 35-44 female 0.051 al024 55-64 male 0.025 al024 35-44 male 0.016 al034 45-54 male 0.021 al036 35-44 male 0.082
i want choose same name of 'interest' column , create new dataset ranked according 'scored.probabilities':
set interest age gender scored.probabilities rank 1 al008 18-24 male 0.211 1 1 al008 35-44 female 0.102 2 1 al008 25-34 female 0.002 3 2 al024 13-17 male 0.102 1 2 al024 35-44 female 0.051 2 2 al024 55-64 male 0.025 3 2 al024 25-34 male 0.022 4 2 al024 35-44 male 0.016 5 3 al034 45-54 male 0.021 1 4 al035 35-44 female 0.027 1 5 al036 35-44 male 0.082 1
you can try
library(data.table) setdt(df1)[order(-scored.probabilities), rank:= 1:.n, interest][ order(interest), set := .grp, interest][order(interest, rank)] # interest age gender scored.probabilities rank set #1: al008 18-24 male 0.211 1 1 #2: al008 35-44 female 0.102 2 1 #3: al008 25-34 female 0.002 3 1 #4: al024 13-17 male 0.102 1 2 #5: al024 35-44 female 0.051 2 2 #6: al024 55-64 male 0.025 3 2 #7: al024 25-34 male 0.022 4 2 #8: al024 35-44 male 0.016 5 2 #9: al034 45-54 male 0.021 1 3 #10: al035 35-44 female 0.027 1 4 #11: al036 35-44 male 0.082 1 5
Comments
Post a Comment