在R中使用while循环更新向量
我得到了以下名为nodes_df的数据帧:在R中使用while循环更新向量,r,dataframe,while-loop,R,Dataframe,While Loop,我得到了以下名为nodes_df的数据帧: x y node_demand 1 2 62 3 2 80 25 14 3 36 88 1 4 57 23 14 5 33 17 19 6 76 43 2 7 77 85 14 8 94 6 6 10 59 72 6 . .
x y node_demand
1 2 62 3
2 80 25 14
3 36 88 1
4 57 23 14
5 33 17 19
6 76 43 2
7 77 85 14
8 94 6 6
10 59 72 6
. . . .
. . . .
. . . .
. . . .
45 60 84 8
46 35 100 5
47 38 2 1
48 9 9 7
50 1 58 2
我必须在集线器和客户端之间拆分此数据帧
hubs <- nodes_df[keep <- sample(1:total_nodes, requested_hubs, replace = FALSE),]
client_nodes <- nodes_df[-keep, ]
循环没有停止,我得到以下返回值:
cumulative_demand
[1] 0 14 20 26 27 35 49 50 68 79 97 100 101 104 109 118
[17] 119 137 150 164 178 185 188 191 208 209 219 222 227 246 252 272 (it carries on and on)
我不知道为什么循环没有停止,尽管条件
累积需求我不得不使用and ifelse()而不是问题描述中所示的if()语句。while循环在条件为真时运行,谢谢!在满足逻辑条件后,我仍然没有设法使循环中断。condition==FALSE
看起来您正在尝试比较某些内容,而不是condition=FALSE
。
cumulative_demand
[1] 0 14 20 26 27 35 49 50 68 79 97 100 101 104 109 118
[17] 119 137 150 164 178 185 188 191 208 209 219 222 227 246 252 272 (it carries on and on)
while(TRUE){
random_clients <- client_nodes[sample(nrow(client_nodes), size = 1, replace = FALSE),]
node_demand <- c(node_demand,random_clients$node_demand)
cumulative_demand <- cumsum(node_demand)
last_node <- (cumulative_demand <= max_supply_capacity)
ifelse(last_node == FALSE,break,next)
}