Count the number of ones (diseased plants) in subareas of a matrix
count_subarea.Rd
This function takes a matrix of 0s and 1s and counts the number of 1s in each subarea of the matrix. The subareas are defined by the number of rows and columns each subarea contains. The function returns a data frame that includes the position (starting row and column) of each subarea and the count of 1s.
Arguments
- data
A matrix of 0s and 1s that you want to analyze.
- rows
The number of rows in each subarea.
- cols
The number of columns in each subarea.
Value
A dataframe with one row for each subarea and the following columns:
Row_Start
: The first row of the subarea in the original matrix.Col_Start
: The first column of the subarea in the original matrix.Ones_Count
: The number of ones in the subarea.
Details
The function as written divides the original matrix into subareas based on the specified number of rows and columns for each subarea and counts the number of ones in each of these subareas. The way it's structured, it handles matrices with even dimensions quite efficiently. However, if the number of rows and columns in the original matrix is not a multiple of the subarea dimensions, some parts of the matrix will not be included in any subarea.
Examples
if (FALSE) {
# Create a sample matrix with random 0s and 1s
set.seed(123) # for reproducibility
my_data <- matrix(sample(c(0, 1), 12*16, replace = TRUE), nrow = 16, ncol = 12)
# Apply the function to count 1's in each 3x3 subarea
result <- count_subarea(data = my_data, rows = 3, cols = 3)
print(result)
# The resulting dataframe can be further used for visualization, for example, with ggplot2.
}