R等级data.frame在不同的类别

问题描述:

我想排名日期在一个date.frame,其中重复获得相同的排名。 Furhter我想区分不同的类别。我没有使用rank函数,因为我得到了漂浮物。R等级data.frame在不同的类别

我的代码正在工作,但非常缓慢。

我该如何让这个更快?

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) 

}

问候

+2

尽量使你的问题重复性,并提供所需的输出的一个例子。 –

这是代码

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 

最佳