有一个矩阵A,如:
1 21 3 49 59
21 33 59 67 65
3 5 7 8 11
21 76 5 4 9
我想只得到每行没有3个或更多数字的行,它们之间都有最大差异< 10
此函数应仅返回第1行。
答案 0 :(得分:5)
这段代码可以。还有一个小例子:
vec <- c(1, 21, 3, 49, 59, 21, 33, 59,
67, 65, 3, 5, 7, 8, 11, 21, 76, 5, 4, 9)
A <- matrix(vec, nrow = 4, byrow = TRUE)
returnRows <- function(x) {
count <- 0
for (i in 1:nrow(x)) {
if (length(which(diff(combn(x[i,],2)) < 10)) < 3) {
count <- c(count,i)
}
}
return(x[count[-1],])
}
## example
returnRows(A)
答案 1 :(得分:3)
您查找所有值组合的diff
,并使用rowSums
进行过滤,即
A[rowSums(t(apply(A, 1, function(i)combn(i, 2, diff))) < 10) < 3,]
#[1] 1 21 3 49 59
答案 2 :(得分:1)
分析两个提交的答案:
输入:
pacman::p_load(microbenchmark)
vec <- c(1, 21, 3, 49, 59, 21, 33, 59,
67, 65, 3, 5, 7, 8, 11, 21, 76, 5, 4, 9)
A <- matrix(vec, nrow = 4, byrow = TRUE)
returnRows <- function(x) {
count <- 0
for (i in 1:nrow(x)) {
if (length(which(diff(combn(x[i,],2)) < 10)) < 3) {
count <- c(count,i)
}
}
return(x[count[-1],])
}
# dvarelas' answer
microbenchmark(returnRows(A))
# Sotos' answer
microbenchmark(A[rowSums(t(apply(A, 1, function(i)combn(i, 2, diff))) < 10) < 3,])
输出:
> microbenchmark(returnRows(A))
Unit: microseconds
expr min lq mean median uq max neval
returnRows(A) 137.113 141.124 152.6586 143.8595 154.7995 301.94 100
> microbenchmark(A[rowSums(t(apply(A, 1, function(i)combn(i, 2, diff))) < 10) < 3,])
Unit: microseconds
expr min lq mean median uq max neval
A[rowSums(t(apply(A, 1, function(i) combn(i, 2, diff))) < 10) < 3, ] 374.507 431.759 645.9935 544.4395 742.45 5324.047 100
结论:dvarelas的答案要快于Sotos的答案