【发布时间】:2019-03-14 10:30:04
【问题描述】:
我有一个数据框“蠕虫”,其中包含两个我感兴趣的用于计算平均值的因素 - 长度相同的“蠕虫密度”和“植被”。 Vegetation 有 5 个数据框,其中我需要 2 个 - 'Meadow' 和 'Grassland' 作为我的平均值,同时还插入一个条件,其中仅包含因素 Area >2.5 和 Soil.Ph>3.5 的结果。是否可以使用tapply 来做到这一点,或者循环是最好的方法吗?我被困在使用 tapply 仅限制我的 Vegetation 因子中的两个数据帧。
从 www.bio.ic.ac.uk/research/crawley/statistics/ 下载的数据文件
worms<-structure(list(Field.Name = structure(c(8L, 17L, 10L, 16L, 7L,
11L, 3L, 1L, 19L, 15L, 5L, 9L, 18L, 12L, 13L, 20L, 2L, 14L, 6L,
4L), .Label = c("Ashurst", "Cheapside", "Church.Field", "Farm.Wood",
"Garden.Wood", "Gravel.Pit", "Gunness.Thicket", "Nashs.Field",
"North.Gravel", "Nursery.Field", "Oak.Mead", "Observatory.Ridge",
"Pond.Field", "Pound.Hill", "Rookery.Slope", "Rush.Meadow", "Silwood.Bottom",
"South.Gravel", "The.Orchard", "Water.Meadow"), class = "factor"),
Area = c(3.6, 5.1, 2.8, 2.4, 3.8, 3.1, 3.5, 2.1, 1.9, 1.5,
2.9, 3.3, 3.7, 1.8, 4.1, 3.9, 2.2, 4.4, 2.9, 0.8), Slope = c(11L,
2L, 3L, 5L, 0L, 2L, 3L, 0L, 0L, 4L, 10L, 1L, 2L, 6L, 0L,
0L, 8L, 2L, 1L, 10L), Vegetation = structure(c(2L, 1L, 2L,
3L, 5L, 2L, 2L, 1L, 4L, 2L, 5L, 2L, 2L, 2L, 3L, 3L, 5L, 1L,
2L, 5L), .Label = c("Arable", "Grassland", "Meadow", "Orchard",
"Scrub"), class = "factor"), Soil.pH = c(4.1, 5.2, 4.3, 4.9,
4.2, 3.9, 4.2, 4.8, 5.7, 5, 5.2, 4.1, 4, 3.8, 5, 4.9, 4.7,
4.5, 3.5, 5.1), Damp = c(FALSE, FALSE, FALSE, TRUE, FALSE,
FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE,
TRUE, TRUE, TRUE, FALSE, FALSE, TRUE), Worm.density = c(4L,
7L, 2L, 5L, 6L, 2L, 3L, 4L, 9L, 7L, 8L, 1L, 2L, 0L, 6L, 8L,
4L, 5L, 1L, 3L)), class = "data.frame", row.names = c(NA,
-20L))
> with(worms,tapply(Worm.density,list[Grassland,Meadow],mean))
sessioninfo()R 版本 3.5.1 (2018-07-02) 平台:x86_64-apple-darwin15.6.0(64位) 运行于:macOS Sierra 10.12.6
矩阵产品:默认 BLAS:/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib LAPACK:/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libLAPACK.dylib
语言环境: [1] C
附加的基础包: [1] stats graphics grDevices utils datasets methods base
其他附加包: [1] RevoUtils_11.0.1
通过命名空间加载(未附加): [1] 编译器_3.5.1 工具_3.5.1
【问题讨论】:
-
请展示一个可重现的小例子和预期的输出
-
添加到问题中 - 我的预期输出将是每个面积 >2.5 和 Soil.pH>3.5 的植被因子中元素 Grassland 和 Meadows 的平均 worm.density
标签: r