Remove clumps of pixels with area smaller than 25 hectares

Using the terra package, I want to remove island pixels (or isolated pixels) from a categorical raster with 1 category. I want to remove pixels with area smaller than 25000 m, given that the pixel size is 10 m. I found the patches() might be suitable for this task.

Below is my raster:
image

r
class : SpatRaster
dimensions : 3115, 2961, 1 (nrow, ncol, nlyr)
resolution : 9.535331, 9.535331 (x, y)
extent : 833145.8, 861379.9, 2690004, 2719707 (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / UTM zone 39N (EPSG:32639)
source(s) : memory
name : b1
min value : 1
max value : 1

Session info:

R version 4.5.0 (2025-04-11 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                           LC_TIME=English_United States.utf8    

time zone: Europe/London
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] terra_1.8-50

loaded via a namespace (and not attached):
[1] compiler_4.5.0    tools_4.5.0       rstudioapi_0.17.1 Rcpp_1.0.14       codetools_0.2-20

Found and adopted the method from this post:

# clumps  using 8-directional connectivity
clumps_r <- patches(r, directions = 8)

# frequency table
clump_freq <- freq(clumps_r) |>
  as.data.frame()

# keep only clumps with count >= 2500 (25,000 m²)
large_clumps <- clump_freq$value[clump_freq$count >= 2500]

# set small clumps to 0
cleaned_r <- clumps_r
cleaned_r[!cleaned_r %in% large_clumps] <- 0

# set large clumps to 1
cleaned_r[cleaned_r != 0] <- 1

# plot
comparison_stack <- c(r, cleaned_r)
plot(comparison_stack)