我想寫一個循環,比較組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
}
我得到一個不同的錯誤:錯誤.subset2(X,I,確切=確切):下標出界。我必須打印什麼?我可以跳過else語句,並且爲每行打印真實條件(中間>其他列)? – user3067851