这是一个复杂的问题。首先,我的for-loop
给出了错误“数据列表中的错误[[i]] <-x:
在输入包含值<1的可迭代序列时,尝试在integerOneIndex中选择少于一个元素”。第二,在将可迭代序列设置为包含值大于等于1时,只能从该范围中获取最大值。当我检查包含值> 1的可迭代序列时,for循环工作正常。
# Example data
df <- data.frame(variable = rep(letters[1:4],3),
SteadyState = sample(0:1,12,replace = T),
measurement = sample(0:20,12))
# Range containing values less than 1
range_less_than_1 <- seq(0.25,0.75,length.out = 4) # Throws error message
# Range containing values >= 1
range_greater_than_1 <- seq(1,1.75,length.out = 4) # Only 1.75 operates
# Range containing values > 1
range_much_greater_than_1 <- seq(2,20,length.out = 4) # Works fine
datalist <- list()
for (i in range_less_than_1){
x <- df %>%
group_by(variable)%>%
summarise(`Range (<)`=i,
TP = length(which(measurement <= i & SteadyState == 1)),
FP = length(which(measurement <= i & SteadyState == 0)),
FN = length(which(measurement > i & SteadyState == 1)),
TN = length(which(measurement > i & SteadyState == 0)))
datalist[[i]] = x
}
df_2 = do.call(rbind,datalist)
所需的输出:
seq(0.25,1.75, length.out=5)
# [1] 0.250 0.625 1.000 1.375 1.750
> df_2
# A tibble: 16 x 6
variable `Range (<)` TP FP FN TN
<fct> <dbl> <int> <int> <int> <int>
1 a 0.25 0 1 2 0
2 b 0.25 0 0 3 0
3 c 0.25 1 0 1 1
4 d 0.25 0 0 2 1
5 a 0.625 1 1 1 0
6 b 0.625 1 0 2 0
7 c 0.625 2 0 0 1
8 d 0.625 0 0 2 1
9 a 1 2 1 0 0
10 b 1 2 0 1 0
11 c 1 2 0 0 1
12 d 1 0 1 2 0
13 a 1.375 2 1 0 0
14 b 1.375 3 0 0 0
15 c 1.375 2 1 0 0
16 d 1.375 2 1 0 0
17 a 1.750 0 1 2 0
18 b 1.750 0 0 3 0
19 c 1.750 0 0 2 1
20 d 1.750 0 0 2 1
答案 0 :(得分:1)
尝试一下-
datalist <- list()
for (i in 1:length(range_less_than_1)){
x <- df %>%
group_by(variable)%>%
summarise(`Range (<)`= range_less_than_1[i],
TP = length(which(measurement <= range_less_than_1[i] & SteadyState == 1)),
FP = length(which(measurement <= range_less_than_1[i] & SteadyState == 0)),
FN = length(which(measurement > range_less_than_1[i] & SteadyState == 1)),
TN = length(which(measurement > range_less_than_1[i] & SteadyState == 0)))
datalist[[i]] = x
}
> df_2 = do.call(rbind,datalist)
> df_2
# A tibble: 16 x 6
variable `Range (<)` TP FP FN TN
<fct> <dbl> <int> <int> <int> <int>
1 a 0.25 0 1 0 2
2 b 0.25 0 0 1 2
3 c 0.25 0 0 2 1
4 d 0.25 0 0 1 2
5 a 0.417 0 1 0 2
6 b 0.417 0 0 1 2
7 c 0.417 0 0 2 1
8 d 0.417 0 0 1 2
9 a 0.583 0 1 0 2
10 b 0.583 0 0 1 2
11 c 0.583 0 0 2 1
12 d 0.583 0 0 1 2
13 a 0.75 0 1 0 2
14 b 0.75 0 0 1 2
15 c 0.75 0 0 2 1
16 d 0.75 0 0 1 2