Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于列值的R中的编号_R_Dataframe - Fatal编程技术网

基于列值的R中的编号

基于列值的R中的编号,r,dataframe,R,Dataframe,我想添加一个新列,Months,从3开始,然后继续它的倍数。所有行都已排序。输出将类似于 Name Grade John C John C+ John C John B John A John A+ Kat B Kat C Kat B Rcode Name Grade Months John C 3 John C+ 6 John C 9 John B 12 John A 15 John A+

我想添加一个新列,
Months
,从3开始,然后继续它的倍数。所有行都已排序。输出将类似于

Name Grade
John   C
John   C+
John   C
John   B
John   A
John   A+
Kat    B
Kat    C
Kat    B
Rcode

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
name试试这个

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
}

您可以对按
Name
分组的3个向量进行累积求和:

library(dplyr)
df1 %>% group_by(Name) %>% mutate(Months=3*seq(n()))

使用
data.table
,将'data.frame'转换为'data.table'(
setDT(df1)
),按'Name'分组,分配(
:=
)3的乘积,并将行序列转换为'Months'

with(df, ave(rep(3, length(Name)), Name, FUN = cumsum))
# [1]  3  6  9 12 15 18  3  6  9

另一种选择,非常类似于Psidom的答案,使用
seq
ave
以及
1:nrow(df)
代替
df$Name
,以避免将字符向量作为输出

library(data.table)
setDT(df1)[, Months := 3* seq_len(.N) , by = Name]
df1
#   Name Grade Months
#1: John     C      3
#2: John    C+      6
#3: John     C      9
#4: John     B     12
#5: John     A     15
#6: John    A+     18
#7:  Kat     B      3
#8:  Kat     C      6
#9:  Kat     B      9
ave(1:nrow(df), df$Name, FUN = seq)*3
# [1]  3  6  9 12 15 18  3  6  9