【发布时间】:2018-11-24 15:27:05
【问题描述】:
我从名为节点的数据帧中采样了 '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, 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))
为了示例,我使用以下代码:
hubs <- nodes[sample(1:total_nodes, hubs_required, replace = FALSE),]
返回:
node_number x y node_demand
33 33 8 26 12
14 14 2 83 6
42 42 41 97 20
13 13 44 76 3
10 10 59 72 6
我想返回所有未被选中的行,以便对它们执行一系列计算。
我认为使用data[-sample,] 之类的东西会起作用,但我收到以下错误
Error in xj[i] : invalid subscript type 'list'.
有谁知道我可以得到这些值吗?
【问题讨论】:
-
寻求帮助时,您应该包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出。你真的试过
data[-sample,]吗?您是创建了一个名为sample的变量还是尝试在那里调用sample()函数? -
我已经更新了代码,希望现在看起来更清晰。我将
sample()函数的结果保存在 hubs 变量中。 -
哦,你的意思是你试过
data[-hubs,]? -
是的,我认为它会起作用,因为
data[-sample,]确实适用于矩阵,但不幸的是它不适用于数据帧。