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.