我的数据如下:
Date ID Value
01-10-2016 1 5
01-10-2016 2 8
01-10-2016 3 7
02-10-2016 1 3
02-10-2016 1 5
02-10-2016 2 8
02-10-2016 3 6
....
我想识别Date和ID列中的值相同的行,并向数据框添加一个额外的列。列值应该等于它重复的次数。
目前,我正在使用for循环。
df$DateID <- paste(df$Date, df$ID) # merge date & ID col
n_occur <- data.frame(table(df$DateID)) # count the no. of occurrences
multi <- c() # create a new vector, to save the indices as many times as the number of occurrences
for (i in 1:nrow(n_occur)) {
multi <- c(multi, rep(n_occur$Freq[i], n_occur$Freq[i]))
}
df <- cbind(df, multi)
输出
Date ID Value multi
01-10-2016 1 5 1
01-10-2016 2 8 1
01-10-2016 3 7 1
02-10-2016 1 3 2
02-10-2016 1 5 2
02-10-2016 2 8 1
02-10-2016 3 6 1
....
我查看了以下帖子,但他们只标记了唯一条目。
Create New Column If Statement Based on Duplicate Rows in R
Find duplicated rows (based on 2 columns) in Data Frame in R