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

Finding the number of 1s connected to 5 or more other 1s (e.g. neighbours) in a number of csv files

$
0
0

I have wrote the below function to find the number of 1s with only one other connecting 1.

temp <- list.files(pattern="*.csv")
files <- lapply(temp, read.table)

number_1_neighbour <- function(x) { 
   check_diff <- diff(x) == 0
   (c(0,check_diff) + c(check_diff,0)) * x 
}

neigh1_rows_columns <- lapply(files, function(y) sum(t(apply(y, 1, 
number_1_neighbour)) + apply(y, 2, number_1_neighbour) == 1))

How would I update this code to check for 1s that have 5 or more connected 1s?

For example in a matrix such as...

0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 1 1
1 1 1 0 0 0 0 0 0 0 
0 0 0 0 1 1 1 0 0 0
0 0 0 0 1 1 1 1 0 0

There would be 3 1s connected by 5 or more other 1s as neighbours.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



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