R 因子子集和条件的tapply vs循环

R 因子子集和条件的tapply vs循环,r,R,我有一个数据框“蠕虫”,其中包含两个我感兴趣的因素,用于计算平均值——“蠕虫密度”和相同长度的“植被”。植被有5个数据帧,其中我需要2-“草地”和“草地”作为我的平均值,同时插入一个条件,该条件仅包括因子面积>2.5和土壤Ph>3.5的结果。有没有可能用tapply实现这一点,或者循环是实现这一点的最佳方式?我被卡住了,只能用tapply限制我的植被因子中的两个数据帧 数据文件从www.bio.ic.ac.uk/research/crawley/statistics下载/ worms<-s

我有一个数据框“蠕虫”,其中包含两个我感兴趣的因素,用于计算平均值——“蠕虫密度”和相同长度的“植被”。植被有5个数据帧,其中我需要2-“草地”和“草地”作为我的平均值,同时插入一个条件,该条件仅包括因子面积>2.5和土壤Ph>3.5的结果。有没有可能用tapply实现这一点,或者循环是实现这一点的最佳方式?我被卡住了,只能用tapply限制我的植被因子中的两个数据帧

数据文件从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))
蠕虫与(蠕虫,tapply(蠕虫。密度,列表[草地,草地],平均值))
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] 统计图形GRUTILS数据集方法库

其他随附包裹: [1] RevoUtils_11.0.1

通过命名空间加载(未附加):
[1] 编译器_3.5.1工具_3.5.1

只需
子集
您的WORM数据帧在
tapply
中,它会产生一个命名向量:

with(subset(worms, Area > 2.5 & Soil.pH > 3.5), 
            tapply(Worm.density, Vegetation, mean)
     )

#   Arable Grassland    Meadow   Orchard     Scrub 
# 6.000000  2.333333  7.000000        NA  7.000000
要仅返回草地和草地,请使用
[]

with(subset(worms, Area > 2.5 & Soil.pH > 3.5), 
            tapply(Worm.density, Vegetation, mean)
     )[c("Grassland", "Meadow")]

# Grassland    Meadow 
#  2.333333  7.000000 
仅对于草地和草地类型的平均值,将其添加到子集,并取蠕虫密度的平均值:

sub_蠕虫2.5和土壤pH>3.5&
植被%c(“草地”、“草地”))
亚Ə虫
#田地。名称区域坡度植被土壤。pH值潮湿蠕虫。密度
#1纳什场3.6 11草地4.1假4
#3苗圃2.8 3草地4.3假2
#6橡树、蜂蜜酒3.1 2草地3.9假2
#7教堂.场地3.5 3草地4.2假3
#12北。砾石3.3 1草地4.1假1
#13南部砾石3.7 2草地4.0假2
#15池塘。田地4.1 0草地5.0真实6
#16水。草地3.9 0草地4.9真实8
平均值(sub_蠕虫$Worm.密度)
# [1] 3.5

请给出一个小的可重复的示例,并将预期输出添加到问题中-我的预期输出将是每个区域植被因子中草地和草地元素的平均密度>2.5和土壤。pH>3.5谢谢,冻糕!我应该说,我只希望有一个数字代表两种植被类型(草地和草甸)中蠕虫密度的平均值,而不是每个植被场的平均值。您不需要
tapply
。只需对两种植被类型进行子集划分并取平均值。请参见编辑。
sub_worms <- subset(worms, Area > 2.5 & Soil.pH > 3.5 &
                           Vegetation %in% c("Grassland", "Meadow"))
sub_worms
#       Field.Name Area Slope Vegetation Soil.pH  Damp Worm.density
# 1    Nashs.Field  3.6    11  Grassland     4.1 FALSE            4
# 3  Nursery.Field  2.8     3  Grassland     4.3 FALSE            2
# 6       Oak.Mead  3.1     2  Grassland     3.9 FALSE            2
# 7   Church.Field  3.5     3  Grassland     4.2 FALSE            3
# 12  North.Gravel  3.3     1  Grassland     4.1 FALSE            1
# 13  South.Gravel  3.7     2  Grassland     4.0 FALSE            2
# 15    Pond.Field  4.1     0     Meadow     5.0  TRUE            6
# 16  Water.Meadow  3.9     0     Meadow     4.9  TRUE            8

mean(sub_worms$Worm.density)
# [1] 3.5