Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206278

How would I find the number of 1s that are connected by 6 or more 1s in a data frame?

$
0
0

I have my attempt to find the number of 1s connected by 6 or more other 1s below...

6_or_more <- function(x) {
  diff <- diff(x) == 0
 (c(0,diff) + c(diff,0)) * x
}

mat <-matrix(rbinom(10 * 5, 1, 0.5), ncol = 20, nrow = 20)
with(rle(as.vector(mat)), sum(lengths[values == 1] >= 5))

6_or_more_rows_columns <- lapply(files, function(y) sum(t(apply(y, 1, 6_or_more)) + apply(y, 2, 6_or_more) >= 6))

6_or_more_neighbours_data <- t(data.frame(6_or_more_rows_columns))
rownames(6_or_more_neighbours_data) <- NULL

6_or_more_neighbours <- 6_or_more_neighbours_data
6_or_more_neighbours

But this is outputted 0 as it is only checking to the top, bottom, left and right of each 1. How would I update this code to get it to check diagonally to each 1 too?


Viewing all articles
Browse latest Browse all 206278

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>