# Inisialisasi data plot_hutan (30 observasi)
set.seed(101)
jenis_pohon <- c("Meranti", "Kapur", "Keruing", "Ulin", "Bengkirai")

plot_hutan <- data.frame(
  no_pohon = 1:30,
  jenis = c(
    "Bengkirai", "Meranti", "Meranti", "Bengkirai", "Ulin",
    "Kapur", "Keruing", "Kapur", "Bengkirai", "Ulin",
    "Kapur", "Bengkirai", "Keruing", "Ulin", "Bengkirai",
    "Kapur", "Bengkirai", "Keruing", "Kapur", "Bengkirai",
    "Ulin", "Kapur", "Keruing", "Ulin", "Bengkirai",
    "Kapur", "Keruing", "Ulin", "Bengkirai", "Meranti"
  ),
  diameter = c(
    48.2, 48.6, 25.3, 22.3, 80.4, 39.1, 53.7, 35.4, 83.2, 61.1,
    82.3, 77.6, 54.0, 66.5, 68.1, 23.0, 82.3, 33.1, 39.4, 78.2,
    56.8, 49.6, 46.0, 25.4, 61.0, 50.2, 57.2, 63.0, 77.1, 44.6
  ),
  tinggi = c(
    31.0, 23.5, 30.9, 23.9, 22.2, 31.9, 22.0, 29.5, 23.8, 35.1,
    26.4, 28.1, 24.7, 30.2, 27.8, 21.5, 29.0, 25.6, 26.8, 32.4,
    28.9, 24.1, 23.4, 20.8, 27.5, 25.9, 26.2, 29.7, 31.4, 24.5
  ),
  stringsAsFactors = FALSE
)

# C1. Struktur dan tipe data
cat("\n===== C1 =====\n")
## 
## ===== C1 =====
str(plot_hutan)
## 'data.frame':    30 obs. of  4 variables:
##  $ no_pohon: int  1 2 3 4 5 6 7 8 9 10 ...
##  $ jenis   : chr  "Bengkirai" "Meranti" "Meranti" "Bengkirai" ...
##  $ diameter: num  48.2 48.6 25.3 22.3 80.4 39.1 53.7 35.4 83.2 61.1 ...
##  $ tinggi  : num  31 23.5 30.9 23.9 22.2 31.9 22 29.5 23.8 35.1 ...
print(vapply(plot_hutan, typeof, character(1)))
##    no_pohon       jenis    diameter      tinggi 
##   "integer" "character"    "double"    "double"
print(vapply(plot_hutan, class, character(1)))
##    no_pohon       jenis    diameter      tinggi 
##   "integer" "character"   "numeric"   "numeric"
# C2. Menghitung LBDS (m^2, diameter dalam cm)
cat("\n===== C2 =====\n")
## 
## ===== C2 =====
plot_hutan$lbds <- round((pi / 4) * (plot_hutan$diameter / 100)^2, 4)
print(plot_hutan[, c("no_pohon", "diameter", "lbds")])
##    no_pohon diameter   lbds
## 1         1     48.2 0.1825
## 2         2     48.6 0.1855
## 3         3     25.3 0.0503
## 4         4     22.3 0.0391
## 5         5     80.4 0.5077
## 6         6     39.1 0.1201
## 7         7     53.7 0.2265
## 8         8     35.4 0.0984
## 9         9     83.2 0.5437
## 10       10     61.1 0.2932
## 11       11     82.3 0.5320
## 12       12     77.6 0.4729
## 13       13     54.0 0.2290
## 14       14     66.5 0.3473
## 15       15     68.1 0.3642
## 16       16     23.0 0.0415
## 17       17     82.3 0.5320
## 18       18     33.1 0.0860
## 19       19     39.4 0.1219
## 20       20     78.2 0.4803
## 21       21     56.8 0.2534
## 22       22     49.6 0.1932
## 23       23     46.0 0.1662
## 24       24     25.4 0.0507
## 25       25     61.0 0.2922
## 26       26     50.2 0.1979
## 27       27     57.2 0.2570
## 28       28     63.0 0.3117
## 29       29     77.1 0.4669
## 30       30     44.6 0.1562
cat("Total LBDS =", round(sum(plot_hutan$lbds), 4), "m2\n")
## Total LBDS = 7.7995 m2
# C3. Pohon layak tebang (diameter >= 50 cm)
cat("\n===== C3 =====\n")
## 
## ===== C3 =====
plot_hutan$layak_tebang <- plot_hutan$diameter >= 50
print(plot_hutan[, c("no_pohon", "diameter", "layak_tebang")])
##    no_pohon diameter layak_tebang
## 1         1     48.2        FALSE
## 2         2     48.6        FALSE
## 3         3     25.3        FALSE
## 4         4     22.3        FALSE
## 5         5     80.4         TRUE
## 6         6     39.1        FALSE
## 7         7     53.7         TRUE
## 8         8     35.4        FALSE
## 9         9     83.2         TRUE
## 10       10     61.1         TRUE
## 11       11     82.3         TRUE
## 12       12     77.6         TRUE
## 13       13     54.0         TRUE
## 14       14     66.5         TRUE
## 15       15     68.1         TRUE
## 16       16     23.0        FALSE
## 17       17     82.3         TRUE
## 18       18     33.1        FALSE
## 19       19     39.4        FALSE
## 20       20     78.2         TRUE
## 21       21     56.8         TRUE
## 22       22     49.6        FALSE
## 23       23     46.0        FALSE
## 24       24     25.4        FALSE
## 25       25     61.0         TRUE
## 26       26     50.2         TRUE
## 27       27     57.2         TRUE
## 28       28     63.0         TRUE
## 29       29     77.1         TRUE
## 30       30     44.6        FALSE
cat("Jumlah pohon layak tebang =", sum(plot_hutan$layak_tebang), "\n")
## Jumlah pohon layak tebang = 17
# C4. Mengubah jenis menjadi factor
cat("\n===== C4 =====\n")
## 
## ===== C4 =====
plot_hutan$jenis <- factor(plot_hutan$jenis, levels = jenis_pohon)
print(class(plot_hutan$jenis))
## [1] "factor"
print(typeof(plot_hutan$jenis))
## [1] "integer"
print(table(plot_hutan$jenis))
## 
##   Meranti     Kapur   Keruing      Ulin Bengkirai 
##         3         7         5         6         9
# C5. Record pohon nomor 1
cat("\n===== C5 =====\n")
## 
## ===== C5 =====
pohon_1 <- list(
  no = plot_hutan$no_pohon[1],
  jenis = plot_hutan$jenis[1],
  diameter = plot_hutan$diameter[1],
  tinggi = plot_hutan$tinggi[1],
  layak = plot_hutan$layak_tebang[1]
)
print(pohon_1$jenis)
## [1] Bengkirai
## Levels: Meranti Kapur Keruing Ulin Bengkirai
print(pohon_1$diameter)
## [1] 48.2
print(pohon_1)
## $no
## [1] 1
## 
## $jenis
## [1] Bengkirai
## Levels: Meranti Kapur Keruing Ulin Bengkirai
## 
## $diameter
## [1] 48.2
## 
## $tinggi
## [1] 31
## 
## $layak
## [1] FALSE