#create a list of data I want to take the grand mean of
A2 <- c(A2_M_fish1, A2_A_fish2, A2_M_fish3, A2_A_fish4)
#create an empty vector to put the grandmeans in
A2_values <- numeric(4)
#make a function to pull out the four last values and get the means
datamean <- function(x)
{
y <- subset(x, Loop>=7 & Loop<=10, select=c(MO2))
mean(y$MO2)
}
因此,在上一个函数中,我引用“循环”,这是我将在循环中使用的每个数据帧中的列的名称。我不知道如何在循环中指定该列,因为如图所示运行它时,它告诉我:
Error in subset.default(x, Loop >= 7 & Loop <= 10, select = c(MO2)) :
object 'Loop' not found
但是当我这样运行时:
datamean <- function(x)
{
y <- subset(x, x$Loop>=7 & x$Loop<=10, select=c(MO2))
mean(y$MO2)
}
这只是给我这个错误:
Error: $ operator is invalid for atomic vectors
答案 0 :(得分:1)
您需要在循环中使用y[, "MO2"]
,而不是像这样的$
:
datamean <- function(x)
{
y <- subset(x, x[ , "Loop"]>=7 & x[ , "Loop"]<=10, select=c("MO2"))
mean(y[ , "MO2"])
}
让我知道您是否仍然遇到问题