对于R中的循环和坐标副本
我想检测序列中的重复坐标:对于R中的循环和坐标副本,r,for-loop,R,For Loop,我想检测序列中的重复坐标: n <- 0 for(ii in 1:11){ for(jj in 1:11){ w <- ii x <- jj y <- ii z <- jj coord1 <- c(w, x) coord2 <- c(y, z) list_coord <- list(coord1, coord2) if(sum(duplicated(list_coord)) > 0) n <- n+1 print(n) }} n重
n <- 0
for(ii in 1:11){
for(jj in 1:11){
w <- ii
x <- jj
y <- ii
z <- jj
coord1 <- c(w, x)
coord2 <- c(y, z)
list_coord <- list(coord1, coord2)
if(sum(duplicated(list_coord)) > 0) n <- n+1
print(n)
}}
n重复的duplicated
语句可以在rbind
ing'coord1',coord2'
n <- 0
for(ii in 1:11){
for(jj in 1:11){
w <- ii
x <- jj
y <- 4
z <- 7
coord1 <- c(w, x)
coord2 <- c(y, z)
new_coord <- rbind(coord1, coord2)
if(sum(duplicated(new_coord)) > 0) n <- n+1
#or
#if(anyDuplicated(new_coord) > 0) n <- n+1
print(n)
}}
n
#[1] 1
n非常感谢akrun,它很有效。你能解释一下为什么它和rbind一起工作而不和list一起工作吗??
n <- 0
for(ii in 1:11){
for(jj in 1:11){
w <- ii
x <- jj
y <- 4
z <- 7
coord1 <- c(w, x)
coord2 <- c(y, z)
new_coord <- rbind(coord1, coord2)
if(sum(duplicated(new_coord)) > 0) n <- n+1
#or
#if(anyDuplicated(new_coord) > 0) n <- n+1
print(n)
}}
n
#[1] 1