【发布时间】:2017-07-14 18:27:30
【问题描述】:
我正在尝试编写一个循环,将组 DT/DF 中的“中间”值与其前面的列进行比较。当循环遇到一个值大于相应“中间”列值的列时,将该列名打印到一个名为 mIncome 的向量中并跳过其余列并继续循环中的下一个迭代。然而,循环似乎并没有结束。
我最终想要一个包含第一列名称的向量,其值大于相应行的“中间”值。我知道不推荐循环,但如果有人有任何建议......
groups <- dput(groups)
structure(list(one = c(33, 32, 161, 93, 69, 74, 24, 24, 21, 25
), two = c(53, 68, 164, 111, 96, 125, 35, 103, 39, 25), three = c(109,
97, 188, 159, 160, 169, 53, 149, 106, 34), four = c(114, 161,
214, 183, 302, 190, 86, 193, 155, 62), five = c(120, 183, 237,
241, 384, 257, 105, 388, 174, 62), six = c(169, 269, 264, 262,
633, 293, 195, 489, 239, 122), seven = c(209, 351, 351, 279,
717, 326, 243, 652, 291, 152), eight = c(214, 393, 357, 346,
769, 336, 255, 672, 353, 197), nine = c(238, 459, 365, 364, 816,
336, 336, 722, 363, 197), middle = c(119, 230, 182, 182, 408,
168, 168, 361, 182, 98)), .Names = c("one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "middle"), class = c("data.table",
"data.frame"), row.names = c(NA, -10L), .internal.selfref = <pointer: 0x00000000000b0788>)
repeat{
mIncome <- character(length = nrow(groups))
for(i in 1:(dim(groups)[1])){
for(j in 1:(dim(groups)[2] - 1)){
if(groups[i][[10]] < groups[i][[j]]){ # is middle value greater than...
mIncome[i] <- as.character(colnames(groups[, j - 1, with = FALSE]))
break
} else (print('no'))
}
}
mIncome
}
【问题讨论】: