R 累计不同数字和重置
我想创建一个关于行中唯一值的两列。另一个是当得到25个不同的值时 让我们举一个例子:R 累计不同数字和重置,r,R,我想创建一个关于行中唯一值的两列。另一个是当得到25个不同的值时 让我们举一个例子: raffle Bola1 Ball2 Ball3 Ball4 Ball5 Ball6 Ball7 Ball8 Ball9 Ball10 Ball11 Ball12 Ball13 Ball14 Ball15 2 23 15 05 04 12 16 20 06 11 19 24 01 09 13 07 3
raffle Bola1 Ball2 Ball3 Ball4 Ball5 Ball6 Ball7 Ball8 Ball9 Ball10 Ball11 Ball12 Ball13 Ball14 Ball15
2 23 15 05 04 12 16 20 06 11 19 24 01 09 13 07
3 20 23 12 08 06 01 07 11 14 04 16 10 09 17 24
4 16 05 25 24 23 08 12 02 17 18 01 10 04 19 13
5 15 13 20 02 11 24 09 16 04 23 25 12 08 19 01
6 23 19 01 05 07 21 16 10 15 25 06 02 12 04 17
7 22 04 15 08 16 14 21 23 12 01 25 19 07 10 18
8 19 16 18 09 13 08 05 25 17 10 06 15 01 22 20
9 21 04 17 05 03 13 16 09 20 24 25 19 11 15 10
10 24 19 08 23 06 02 20 11 09 03 04 10 05 12 14
11 24 09 08 19 20 22 06 10 11 16 07 25 23 02 12
12 11 05 25 01 09 08 16 04 07 24 17 02 12 14 10
13 13 06 10 05 08 14 03 11 16 15 09 17 19 07 23
14 14 21 13 19 20 06 09 05 07 23 18 01 15 02 25
15 23 06 21 04 10 24 16 01 15 02 08 19 12 18 25
16 24 17 05 08 07 12 13 02 15 10 19 25 23 21 06
17 13 20 17 01 06 07 02 14 05 09 16 19 03 21 18
18 02 23 10 07 11 14 17 22 15 06 24 08 19 20 18
19 15 17 10 23 11 24 13 14 06 02 08 05 20 16 07
20 04 09 08 24 16 20 03 17 18 19 07 06 23 14 10
21 05 02 01 22 19 08 24 04 25 23 18 20 14 11 16
22 13 15 05 09 07 10 01 03 22 02 25 14 06 04 12
23 10 11 05 19 18 14 06 04 20 01 08 03 12 16 17
24 01 19 21 14 02 23 25 05 20 11 07 10 24 17 03
25 04 23 20 02 05 13 07 09 24 03 01 06 14 22 16
26 19 11 07 16 08 21 05 10 20 13 23 09 17 14 22
27 25 06 22 21 11 24 03 14 12 13 20 08 10 15 18
28 18 21 11 07 09 03 20 16 14 12 13 17 01 19 10
29 13 14 06 01 24 04 08 05 17 22 21 19 20 09 16
30 22 02 01 17 08 04 19 20 11 14 06 21 07 23 03
我有15个不同的值,在第一行,
我在第二行有6个不同的值,
我有3个不同的值,在第三行
在第七行,我完成了所有数字,25个不同的值
我需要记住这些信息,就像这样
raffle Ball1 Ball15 unique_balls group
1 16 02 15 1
2 22 19 21 1
...
7 24 10 25 1
8 8 1 15 2
当我得到25个不同的值时,我表示另一组
我有超过100张抽奖券,帮帮我 如果要计算每行中的唯一值,并将其向前推进,直到达到
阈值,我们可以使用for
循环
num <- numeric(length = 0L) #Vector to store unique values
threshold <- 25 #Threshold value to reset
df$group <- 1 #Initialise all group values to 1
count <- 1 #Variable to keep the count of unique groups
#For every row in the dataframe
for (i in seq_len(nrow(df))) {
#Get all the unique values from previous rows before threshold was reached
#and append new unique values for this row
num <- unique(c(num, as.integer(df[i, ])))
#If the length of unique values reaches the threshold
if (length(num) >= threshold) {
df$group[i] <- count
#Empty the unique values vector
num <- numeric(length = 0L)
#Increment the group count by 1
count = count + 1
}
else {
#If the threshold is not reached, continue the previous count
df$group[i] <- count
}
}
df$group
# [1] 1 1 1 1 1 1 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 7
numtrycumsum(apply(df1[-1],1,函数(x)length(unique(x)))
对不起,我需要使用行来完成unique,结果只有15..因此,我需要结束一个循环!我需要使用行完成25个不同的值!当我得到它们时,我需要重新设置id,再次计数!什么不清楚?对不起,我把问题搞错了!尝试df2