R:基于多列的聚合
R: aggregate based of multiple columns
如何通过Tree.Species隔间、林分、横断面和样地求和胸径和底面积?
Tree.Species DBH Basal Area Compartment Stand Transect Plot
Sugar Maple 16.4 211.1336 107 20 2 3
Sugar Maple 25.1 494.55785 107 20 2 3
Hemlock 15.1 178.98785 209 30 1 2
我正在尝试:
aggregate(.~ Compartment + Stand + Transect + Plot + Tree.Speices, data = Trees, FUN = sum)
但我不断收到此错误:
Error in model.frame.default(formula = cbind(DBH, Basal.Area, Transect.., :
variable lengths differ (found for 'Transect')
我的变量值是唯一的,数据中没有所有可能的组合。
Compartment: 107 209 310 231
Stand: 20 110 30 240 80 300
Transect: 1 2 3
Plot: 1 2 3 4 5 6
输出:
structure(list(Tree.Speices = structure(c(53L, 49L, 49L, 49L,
49L, 11L, 49L, 12L, 49L, 4L, 49L, 49L, 49L, 53L, 49L, 49L, 4L,
4L, 33L, 4L, 11L, 53L, 11L, 53L, 53L, 21L, 21L, 53L, 49L, 53L,
49L, 49L, 53L, 21L, 4L, 4L, 49L, 12L, 21L, 49L, 49L, 49L, 49L,
9L, 49L, 49L, 11L, 11L, 53L, 47L, 33L, 11L, 5L, 49L, 11L, 11L,
38L, 11L, 49L, 11L, 11L, 11L, 11L, 49L, 53L, 53L, 53L, 47L, 49L,
49L, 49L, 47L, 49L, 33L, 4L, 4L, 47L, 4L, 11L, 49L, 53L, 49L,
11L, 11L, 11L, 49L, 11L, 11L, 11L, 11L, 11L, 49L, 11L, 49L, 47L,
49L, 11L, 11L, 11L, 11L, 21L), .Label = c("American Elm", "Aspen",
"Balsam", "Basswood", "Beech", "Big-Toothed Aspen", "Black Cherry",
"Cedar", "Cottonwood", "Elm", "Hemlock", "Hop Hornbeam", "paper birch",
"Paper Birch", "Poplar", "Quaking Aspen", "Red Maple", "Red Oak",
"Red Spruce", "snag", "Snag", "Snag (Aspen)", "Snag (Basswood)",
"Snag (Beech)", "Snag (Big-Toothed Aspen)", "SNAG (Big-Toothed Aspen)",
"snag (conifer)", "Snag (Conifer)", "Snag (Cottonwood)", "Snag (Elm)",
"Snag (hardwood)", "snag (Hemlock)", "Snag (Hemlock)", "SNAG (Hemlock)",
"Snag (maple)", "Snag (Maple)", "Snag (Oak)", "Snag (Paper Birch)",
"Snag (Poplar)", "Snag (Red Maple)", "snag (Sugar Maple)", "Snag (Sugar Maple)",
"Snag (Sugar)", "Snag (White Ash)", "Snag (White Pine)", "snag (Yellow Birch)",
"Snag (Yellow Birch)", "SNAG (Yellow Birch)", "Sugar Maple",
"White Ash", "White Birch", "White Pine", "Yellow Birch"), class = "factor"),
DBH = c(55.7, 21.3, 14, 38, 6.5, 20.3, 33.2, 6.3, 30.5, 22.3,
32.7, 8.9, 41.8, 30, 24.6, 13.8, 56.6, 49.5, 49.9, 63.2,
28, 39, 21, 25.7, 29.9, 38, 17.5, 22.4, 6.2, 20.3, 18.3,
21, 24.7, 49.5, 6.4, 30.3, 19.4, 6, 37.8, 24.6, 24.4, 9.5,
17.4, 49.2, 26, 31, 23.6, 19.8, 37.9, 25.8, 31.5, 18.1, 34.4,
59.7, 28.2, 21, 16.4, 23.7, 34.4, 24.7, 15.4, 12, 24.2, 34.2,
19.4, 15.1, 34.9, 34.8, 6.6, 61.2, 25.4, 38.8, 28.9, 32.3,
43.9, 33.8, 27.1, 37, 21.2, 26.4, 27.4, 10.6, 55.1, 69.4,
24, 25.4, 51, 20.2, 14.3, 31.8, 48.8, 38.3, 19.6, 26.3, 34.5,
6.3, 41.3, 32.6, 14.6, 9.1, 57.8), Basal.Area = c(2435.45465,
356.14665, 153.86, 1133.54, 33.16625, 323.49065, 865.2584,
31.15665, 730.24625, 390.37265, 839.39265, 62.17985, 1371.5834,
706.5, 475.0506, 149.4954, 2514.7946, 1923.44625, 1954.65785,
3135.4784, 615.44, 1193.985, 346.185, 518.48465, 701.79785,
1133.54, 240.40625, 393.8816, 30.1754, 323.49065, 262.88865,
346.185, 478.92065, 1923.44625, 32.1536, 720.70065, 295.4426,
28.26, 1121.6394, 475.0506, 467.3576, 70.84625, 237.6666,
1900.2024, 530.66, 754.385, 437.2136, 307.7514, 1127.58185,
522.5274, 778.91625, 257.17385, 928.9376, 2797.81065, 624.2634,
346.185, 211.1336, 440.92665, 928.9376, 478.92065, 186.1706,
113.04, 459.7274, 918.1674, 295.4426, 178.98785, 956.13785,
950.6664, 34.1946, 2940.1704, 506.4506, 1181.7704, 655.63985,
818.98265, 1512.85985, 896.8154, 576.51185, 1074.665, 352.8104,
547.1136, 589.3466, 88.2026, 2383.26785, 3780.8426, 452.16,
506.4506, 2041.785, 320.3114, 160.52465, 793.8234, 1869.4304,
1151.50865, 301.5656, 542.97665, 934.34625, 31.15665, 1338.96665,
834.2666, 167.3306, 65.00585, 2622.5594), Compartment = c(107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L
), Stand = c(20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L,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Compartment", "Stand", "Transect..", "Plot.."
), row.names = 67:167, class = "data.frame")
最后两列名称末尾有两个点,Species 拼写错误:
> names(Trees)
[1] "Tree.Speices" "DBH" "Basal.Area" "Compartment" "Stand"
[6] "Transect.." "Plot.."
尝试:
aggregate(.~ Compartment + Stand + Transect.. + Plot.. + Tree.Speices,
data = Trees, FUN = sum)
或删除所有名称末尾的点并更正拼写:
names(Trees) <- sub("\.+$", "", names(Trees))
names(Trees) <- sub("Speices", "Species", names(Trees))
aggregate(.~ Compartment + Stand + Transect + Plot + Tree.Species,
data = Trees, FUN = sum)
如何通过Tree.Species隔间、林分、横断面和样地求和胸径和底面积?
Tree.Species DBH Basal Area Compartment Stand Transect Plot
Sugar Maple 16.4 211.1336 107 20 2 3
Sugar Maple 25.1 494.55785 107 20 2 3
Hemlock 15.1 178.98785 209 30 1 2
我正在尝试:
aggregate(.~ Compartment + Stand + Transect + Plot + Tree.Speices, data = Trees, FUN = sum)
但我不断收到此错误:
Error in model.frame.default(formula = cbind(DBH, Basal.Area, Transect.., :
variable lengths differ (found for 'Transect')
我的变量值是唯一的,数据中没有所有可能的组合。
Compartment: 107 209 310 231
Stand: 20 110 30 240 80 300
Transect: 1 2 3
Plot: 1 2 3 4 5 6
输出:
structure(list(Tree.Speices = structure(c(53L, 49L, 49L, 49L,
49L, 11L, 49L, 12L, 49L, 4L, 49L, 49L, 49L, 53L, 49L, 49L, 4L,
4L, 33L, 4L, 11L, 53L, 11L, 53L, 53L, 21L, 21L, 53L, 49L, 53L,
49L, 49L, 53L, 21L, 4L, 4L, 49L, 12L, 21L, 49L, 49L, 49L, 49L,
9L, 49L, 49L, 11L, 11L, 53L, 47L, 33L, 11L, 5L, 49L, 11L, 11L,
38L, 11L, 49L, 11L, 11L, 11L, 11L, 49L, 53L, 53L, 53L, 47L, 49L,
49L, 49L, 47L, 49L, 33L, 4L, 4L, 47L, 4L, 11L, 49L, 53L, 49L,
11L, 11L, 11L, 49L, 11L, 11L, 11L, 11L, 11L, 49L, 11L, 49L, 47L,
49L, 11L, 11L, 11L, 11L, 21L), .Label = c("American Elm", "Aspen",
"Balsam", "Basswood", "Beech", "Big-Toothed Aspen", "Black Cherry",
"Cedar", "Cottonwood", "Elm", "Hemlock", "Hop Hornbeam", "paper birch",
"Paper Birch", "Poplar", "Quaking Aspen", "Red Maple", "Red Oak",
"Red Spruce", "snag", "Snag", "Snag (Aspen)", "Snag (Basswood)",
"Snag (Beech)", "Snag (Big-Toothed Aspen)", "SNAG (Big-Toothed Aspen)",
"snag (conifer)", "Snag (Conifer)", "Snag (Cottonwood)", "Snag (Elm)",
"Snag (hardwood)", "snag (Hemlock)", "Snag (Hemlock)", "SNAG (Hemlock)",
"Snag (maple)", "Snag (Maple)", "Snag (Oak)", "Snag (Paper Birch)",
"Snag (Poplar)", "Snag (Red Maple)", "snag (Sugar Maple)", "Snag (Sugar Maple)",
"Snag (Sugar)", "Snag (White Ash)", "Snag (White Pine)", "snag (Yellow Birch)",
"Snag (Yellow Birch)", "SNAG (Yellow Birch)", "Sugar Maple",
"White Ash", "White Birch", "White Pine", "Yellow Birch"), class = "factor"),
DBH = c(55.7, 21.3, 14, 38, 6.5, 20.3, 33.2, 6.3, 30.5, 22.3,
32.7, 8.9, 41.8, 30, 24.6, 13.8, 56.6, 49.5, 49.9, 63.2,
28, 39, 21, 25.7, 29.9, 38, 17.5, 22.4, 6.2, 20.3, 18.3,
21, 24.7, 49.5, 6.4, 30.3, 19.4, 6, 37.8, 24.6, 24.4, 9.5,
17.4, 49.2, 26, 31, 23.6, 19.8, 37.9, 25.8, 31.5, 18.1, 34.4,
59.7, 28.2, 21, 16.4, 23.7, 34.4, 24.7, 15.4, 12, 24.2, 34.2,
19.4, 15.1, 34.9, 34.8, 6.6, 61.2, 25.4, 38.8, 28.9, 32.3,
43.9, 33.8, 27.1, 37, 21.2, 26.4, 27.4, 10.6, 55.1, 69.4,
24, 25.4, 51, 20.2, 14.3, 31.8, 48.8, 38.3, 19.6, 26.3, 34.5,
6.3, 41.3, 32.6, 14.6, 9.1, 57.8), Basal.Area = c(2435.45465,
356.14665, 153.86, 1133.54, 33.16625, 323.49065, 865.2584,
31.15665, 730.24625, 390.37265, 839.39265, 62.17985, 1371.5834,
706.5, 475.0506, 149.4954, 2514.7946, 1923.44625, 1954.65785,
3135.4784, 615.44, 1193.985, 346.185, 518.48465, 701.79785,
1133.54, 240.40625, 393.8816, 30.1754, 323.49065, 262.88865,
346.185, 478.92065, 1923.44625, 32.1536, 720.70065, 295.4426,
28.26, 1121.6394, 475.0506, 467.3576, 70.84625, 237.6666,
1900.2024, 530.66, 754.385, 437.2136, 307.7514, 1127.58185,
522.5274, 778.91625, 257.17385, 928.9376, 2797.81065, 624.2634,
346.185, 211.1336, 440.92665, 928.9376, 478.92065, 186.1706,
113.04, 459.7274, 918.1674, 295.4426, 178.98785, 956.13785,
950.6664, 34.1946, 2940.1704, 506.4506, 1181.7704, 655.63985,
818.98265, 1512.85985, 896.8154, 576.51185, 1074.665, 352.8104,
547.1136, 589.3466, 88.2026, 2383.26785, 3780.8426, 452.16,
506.4506, 2041.785, 320.3114, 160.52465, 793.8234, 1869.4304,
1151.50865, 301.5656, 542.97665, 934.34625, 31.15665, 1338.96665,
834.2666, 167.3306, 65.00585, 2622.5594), Compartment = c(107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L,
107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L, 107L
), Stand = c(20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L), Transect.. = c(2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Plot.. = c(1L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("Tree.Speices",
"DBH", "Basal.Area", "Compartment", "Stand", "Transect..", "Plot.."
), row.names = 67:167, class = "data.frame")
最后两列名称末尾有两个点,Species 拼写错误:
> names(Trees)
[1] "Tree.Speices" "DBH" "Basal.Area" "Compartment" "Stand"
[6] "Transect.." "Plot.."
尝试:
aggregate(.~ Compartment + Stand + Transect.. + Plot.. + Tree.Speices,
data = Trees, FUN = sum)
或删除所有名称末尾的点并更正拼写:
names(Trees) <- sub("\.+$", "", names(Trees))
names(Trees) <- sub("Speices", "Species", names(Trees))
aggregate(.~ Compartment + Stand + Transect + Plot + Tree.Species,
data = Trees, FUN = sum)