Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
取依赖于R中同一行中多个其他列的一组数据的平均值_R_Multiple Columns_Mean - Fatal编程技术网

取依赖于R中同一行中多个其他列的一组数据的平均值

取依赖于R中同一行中多个其他列的一组数据的平均值,r,multiple-columns,mean,R,Multiple Columns,Mean,我想每4个样方取动物数量的平均值。站点和区域控制应匹配平均样方组 对R来说相当陌生 我的尝试: 错误:拆分器\u a.data、.margins、.expand中出错: “pairlist”对象不能强制为“integer”类型 我认为你想做的事情可以简单地像这样完成: 如果您有: commData <- structure(list(areaContro = c(29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,

我想每4个样方取动物数量的平均值。站点和区域控制应匹配平均样方组

对R来说相当陌生

我的尝试:

错误:拆分器\u a.data、.margins、.expand中出错: “pairlist”对象不能强制为“integer”类型


我认为你想做的事情可以简单地像这样完成:

如果您有:

commData <- 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")
对于所有请求的变量:

full_scallop <- aggregate(commData$scallops, by = list(commData$quadrat, commData$areaContro, commData$station), FUN = mean)
如果您是R新手,我强烈建议您看看dplyr,尤其是用于常见数据操作任务的dplyr

你的第二个aaply参数是不正确的。根据文档,它接受给定下标的向量来分割数据,例如1行。还要注意,它接受一个数组并生成一个数组

我不知道你想平均哪些变量,平均值应该以什么为条件。我想你想要的是按站点、样方和面积控制分组的平均值,但这是常数

基准R:

dplyr:

按站平均
commData <- 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")
scallop <- aggregate(commData$scallops, by = list(commData$quadrat), FUN = mean)
full_scallop <- aggregate(commData$scallops, by = list(commData$quadrat, commData$areaContro, commData$station), FUN = mean)
aggregate(cbind(commData$scallops, commData$clappers)~commData$quadrat+commData$areaContro+commData$station,  FUN = mean)
tapply(data$scallops, data[c("station", "quadrat")], mean)
data %>% group_by(station, quadrat) %>% 
summarise(scallops_mean = mean(scallops))