在R语言中统计栅格数据唯一值的出现次数
Hey there! Let's sort out this efficient counting of unique raster values for you—since you mentioned your current method is tedious and doesn't scale well for multiple rasters, here are some straightforward solutions.
The raster package has a built-in function freq() that's perfect for this—it directly calculates the occurrence of each non-NA value in your raster, no messy vector conversions required. Here's how to use it:
# Load your raster MyRaster1 <- raster("MyRaster_EUNIS1.tif") # Generate a frequency table (automatically ignores NA values) value_counts <- freq(MyRaster1) # If you want to filter to only the specific unique values you identified (3,7,9,10) filtered_counts <- value_counts[value_counts[,1] %in% c(3,7,9,10), ] # Add readable column names colnames(filtered_counts) <- c("Value", "Count") # View the results print(filtered_counts)
This method is fast, memory-efficient (especially for large rasters), and way simpler than converting to factors manually.
If you need to run this across a folder of raster files, you can wrap the logic in a loop. This will automatically process each raster, count its unique values, and compile all results into a single dataframe:
# Get a list of all your TIFF raster files (adjust the path and pattern as needed) raster_files <- list.files(path = "./your_raster_directory", pattern = "\\.tif$", full.names = TRUE) # Create an empty dataframe to store all results all_raster_counts <- data.frame( Raster_File = character(), Unique_Value = numeric(), Occurrence_Count = integer(), stringsAsFactors = FALSE ) # Loop through each raster file for (file_path in raster_files) { # Load the current raster current_raster <- raster(file_path) # Get unique non-NA values for the current raster current_unique <- unique(current_raster)[!is.na(unique(current_raster))] # Get frequency counts current_counts <- freq(current_raster) # Filter to only the unique values present in this raster filtered_current_counts <- current_counts[current_counts[,1] %in% current_unique, ] # Create a temporary dataframe for this raster's results temp_df <- data.frame( Raster_File = basename(file_path), Unique_Value = filtered_current_counts[,1], Occurrence_Count = filtered_current_counts[,2], stringsAsFactors = FALSE ) # Append to the main results dataframe all_raster_counts <- rbind(all_raster_counts, temp_df) } # View the combined results across all rasters print(all_raster_counts)
This loop works regardless of the unique values in each raster—no manual adjustments needed for different datasets.
For context, the tedious factor-based approach you started might look something like this:
# The manual, non-scalable method raster_vector <- values(MyRaster1) raster_vector_no_na <- raster_vector[!is.na(raster_vector)] Classes1 <- as.factor(raster_vector_no_na) tedious_counts <- table(Classes1)
The problem here is that converting large rasters to vectors can eat up memory, and you'd have to rewrite this logic for every single raster. Using freq() skips the vector conversion entirely and is optimized for raster data.
内容的提问来源于stack exchange,提问作者viomouse

