我正在尝试在date.frame中对日期进行排名,其中重复项的排名相同。我想区分不同的类别。我没有使用rank函数,因为我得到了浮点数。
我的代码工作但速度非常慢。
如何让它更快?
for(i in 1:length(unique_IDS)){
temp_df=data.frame(rounds=unique(df$Dates[rounds$ID==unique_IDS[i]]), round_number=0)
temp_df=temp_df[order(temp_df$rounds),]
#temp_df$round_number=1:nrow(temp_df)
df$round_number[df$ID==unique_IDS[i]]= match(x = df$Dates[df$ID==unique_IDS[i]],table=temp_df$rounds)
}
最好的问候
答案 0 :(得分:0)
这是代码
的一个例子df=data.frame(x=runif(10, 1, 10), cat="A", stringsAsFactors = F)
df$cat[3:5]="B"
df$cat[6:10]="C"
df$rank=0
cats=unique(df$cat)
for(i in 1:length(cats)){
temp=df$x[df$cat==cats[i]]
temp=temp[order(temp)]
df$rank[df$cat==cats[i]]=match(df$x[df$cat==cats[i]], table = temp)
}
df
最佳