1
votes

I've got a data frame 'worms' with two factors I am interested in for calculating the average - 'worm.density' and 'Vegetation' with the same length. Vegetation has 5 data frames of which I need 2 -'Meadow' and 'Grassland' for my average while also inserting a condition that includes only results of factors Area >2.5 and Soil.Ph>3.5. Is it possible to do this with tapply or are loops the best way for this? I got stuck limiting only the two data frames from my Vegetation factor with tapply.

datafile downloaded from 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 version 3.5.1 (2018-07-02) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS Sierra 10.12.6

Matrix products: default 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

locale: [1] C

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

other attached packages: [1] RevoUtils_11.0.1

loaded via a namespace (and not attached): [1] compiler_3.5.1 tools_3.5.1

1
Please show a small reproducible example and expected outputakrun
Added to the question - my expected output would be the average worm.density of elements Grassland and Meadows in Vegetation factor for every Area >2.5 and Soil.pH>3.5SFSN

1 Answers

1
votes

Simply subset your worms data frame inside tapply which results in a named vector:

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

To only return Grassland and Meadow, index the items with []

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

# Grassland    Meadow 
#  2.333333  7.000000 

For only Grassland and Meadow type means, add that to subset and take mean of Worm.density:

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

Rextester demo