Taking the mean of a group of data that is dependent on multiple other columns in the same row in R -
i want take mean of animal abundance every 4 quadrats. station # , areacontro # should match averaged groups of quadrats
fairly new r
my attempt:
aaply(commdata, station ~ areacontro & quadrat ~ station, .fun = mean, .expand = true,.inform = true, .drop = true)
the error: error in splitter_a(.data, .margins, .expand) :
'pairlist' object cannot coerced type 'integer'
structure(list(areacontro = c(29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l, 29l), station = c(1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 4l, 4l, 4l, 4l, 5l, 5l, 5l, 5l, 6l, 6l, 6l, 6l, 7l, 7l, 7l, 7l, 8l, 8l), quadrat = c(1l, 2l, 3l, 4l, 1l, 2l, 3l, 4l, 1l, 2l, 3l, 4l, 1l, 2l, 3l, 4l, 1l, 2l, 3l, 4l, 1l, 2l, 3l, 4l, 1l, 2l, 3l, 4l, 1l, 2l), latitude = c(42.12521667, 42.12658333, 42.12681667, 42.12705, 42.12466667, 42.12631667, 42.12671667, 42.1272, 42.12671667, 42.12682833, 42.12726166, 42.12794499, 42.12771667, 42.1285, 42.12871667, 42.12896667, 42.12691667, 42.12748333, 42.12763333, 42.12785, 42.127, 42.12711818, 42.12735152, 42.12755152, 42.1264341, 42.1265095, 42.12664427, 42.12679211, 42.12703333, 42.12725), longitude = c(-67.33001667, -67.32823333, -67.3281, -67.3279, -67.31041667, -67.30906667, -67.30876667, -67.30843333, -67.29326667, -67.2942027, -67.29311937, -67.2929027, -67.27731667, -67.2768, -67.27655, -67.27628333, -67.25879572, -67.25684572, -67.25647905, -67.25616238, -67.2359, -67.23562265, -67.23512265, -67.23472265, -67.21841245, -67.21825004, -67.21814781, -67.21796007, -67.19853333, -67.19653333), scallops = c(1l, 0l, 0l, 0l, 4l, 0l, 7l, 3l, 3l, 3l, 1l, 2l, 2l, 1l, 2l, 0l, 2l, 2l, 2l, 2l, 45l, 11l, 4l, 8l, 12l, 9l, 11l, 11l, 4l, 10l), clappers = c(0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 0l, 2l, 0l, 0l, 0l, 0l)), .names = c("areacontro", "station", "quadrat", "latitude", "longitude", "scallops", "clappers" ), row.names = c(na, 30l), class = "data.frame")
if new r
recommend taking @ tidyverse in particular dplyr
common data manipulation tasks.
your second argument of aaply incorrect. according documentation accepts vector given subscripts split data (e.g. 1 rows). note accepts array , results in array.
i'm confused variable(s) want average on , average should conditioned on. think want average grouped station
, quadrat
(and areacontro
constant)
base r
:
tapply(data$scallops, data[c("station", "quadrat")], mean)
dplyr
:
data %>% group_by(station, quadrat) %>% summarise(scallops_mean = mean(scallops))
Comments
Post a Comment