从R中的数据帧进行子集设置
我从名为节点的数据帧中采样了“n”行:从R中的数据帧进行子集设置,r,dataframe,random,subset,R,Dataframe,Random,Subset,我从名为节点的数据帧中采样了“n”行: nodes <- structure(list(node_number = 1:50, x = c(2L, 80L, 36L, 57L, 33L, 76L, 77L, 94L, 89L, 59L, 39L, 87L, 44L, 2L, 19L, 5L, 58L, 14L, 43L
nodes <- structure(list(node_number = 1:50,
x = c(2L, 80L, 36L, 57L, 33L, 76L, 77L, 94L,
89L, 59L, 39L, 87L, 44L, 2L, 19L, 5L,
58L, 14L, 43L, 87L, 11L, 31L, 51L, 55L,
84L, 12L, 53L, 53L, 33L, 69L, 43L, 10L,
8L, 3L, 96L, 6L, 59L, 66L, 22L, 75L, 4L,
41L, 92L, 12L, 60L, 35L, 38L, 9L, 54L, 1L),
y = c(62L, 25L, 88L, 23L, 17L, 43L, 85L, 6L, 11L,
72L, 82L, 24L, 76L, 83L, 43L, 27L, 72L, 50L,
18L, 7L, 56L, 16L, 94L, 13L, 57L, 2L, 33L, 10L,
32L, 67L, 5L, 75L, 26L, 1L, 22L, 48L, 22L, 69L,
50L, 21L, 81L, 97L, 34L, 64L, 84L, 100L, 2L, 9L, 59L, 58L),
node_demand = c(3L, 14L, 1L, 14L, 19L, 2L, 14L, 6L,
7L, 6L, 10L, 18L, 3L, 6L, 20L, 4L,
14L, 11L, 19L, 15L, 15L, 4L, 13L,
13L, 5L, 16L, 3L, 7L, 14L, 17L,
3L, 3L, 12L, 14L, 20L, 13L, 10L,
9L, 6L, 18L, 7L, 20L, 9L, 1L, 8L,
5L, 1L, 7L, 9L, 2L)),
.Names = c("node_number", "x", "y", "node_demand"),
class = "data.frame", row.names = c(NA, -50L))
我想返回所有尚未选择的行,以便对它们执行一系列计算
我认为使用像data[-sample,]
这样的东西可以工作,但是我得到了以下错误
xj[i]中出现错误:下标类型“列表”无效。
有人知道谁可以得到这些值吗?保存所选索引的列表会更容易。糟糕的
hubs <- nodes[keep <- sample(1:total_nodes, hubs_required, replace = FALSE),]
other_hubs <- nodes[-keep, ]
other_hubs <- nodes[nodes%node_number %in% hubs$node_number, ]
在寻求帮助时,您应该包括一个简单的示例输入和所需的输出,可用于测试和验证可能的解决方案。您是否确实尝试了
数据[-sample,]
?您是创建了名为sample
的变量,还是试图在那里调用sample()
函数?我已经更新了代码,希望现在代码看起来更清晰。我将sample()
函数的结果保存在hubs变量中。哦,你的意思是你尝试了data[-hubs,]
?是的,我认为它会起作用,因为data[-sample,]
确实适用于矩阵,但不幸的是,数据帧不是这样。谢谢!第一个很好用!。我很惊讶,虽然语法是相同的,即nodes[-keep,]
它没有抛出错误消息。是因为变量被初始化为子集吗<代码>节点[保留]
hubs <- nodes[keep <- sample(1:total_nodes, hubs_required, replace = FALSE),]
other_hubs <- nodes[-keep, ]
other_hubs <- nodes[nodes%node_number %in% hubs$node_number, ]
nodes %>% anti_join(hubs, by="node_number")