Name Grade
John C
John C+
John C
John B
John A
John A+
Kat B
Kat C
Kat B
我想添加一個新列Months
,從3開始,然後繼續其倍數。行全部排序。輸出看起來像基於列值的R中的編號
Name Grade Months
John C 3
John C+ 6
John C 9
John B 12
John A 15
John A+ 18
Kat B 3
Kat C 6
Kat B 9
RCODE
name <- df$Name[1]
count <- 0
for (i in 1:length(df[,1])){
if (name!=df$Name[i]){
count <- 0
name <- df$Name[i]
}
df$Months[i] <- count
count <- count + 3
}
我可以不用一個循環?