我的问题是关于访问我的数据帧特定部分的$运算符。
以下是我的数据框
structure(list(Treatment = structure(c(11L, 11L, 11L, 11L, 11L,
11L), .Label = c("Chlora", "Gen", "Lin", "Metro", "Metro+Pen",
"Metro+Rif", "Metro+Rif+Pen", "Pen", "Pen+Rif", "Rif", "Untreated"
), class = "factor"), CDW = c(0.833, 0.867, 0.833, 2.7, 3.3,
3.133), Chlorophyll = c(1.098, 1.376, 1.64, 34.714, 37.773, 40.54
), EPS = c(0.092, 0.067, 0.092, 0.571, 0.591, 0.597), Day = c(0L,
0L, 0L, 10L, 10L, 10L), TreatmentDay = structure(c(21L, 21L,
21L, 22L, 22L, 22L), .Label = c("Chlora0", "Chlora10", "Gen0",
"Gen10", "Lin0", "Lin10", "Metro+Pen0", "Metro+Pen10", "Metro+Rif+Pen0",
"Metro+Rif+Pen10", "Metro+Rif0", "Metro+Rif10", "Metro0", "Metro10",
"Pen+Rif0", "Pen+Rif10", "Pen0", "Pen10", "Rif0", "Rif10", "Untreated0",
"Untreated10"), class = "factor"), richness = c(36L, 37L, 39L,
31L, 31L, 24L), shannon = c(3.35867443756547, 3.37354007038303,
3.43640985397324, 3.16018680691703, 3.15718684762791, 2.85288614815093
)), .Names = c("Treatment", "CDW", "Chlorophyll", "EPS", "Day",
"TreatmentDay", "richness", "shannon"), row.names = c("Day0F1",
"Day0F2", "Day0F3", "Day10F1", "Day10F2", "Day10F3"), class = "data.frame")
我可以用
Unt$richness
[1] 36 37 39 31 31 24
但是,如果我想进行anova测试,我会收到一条错误消息,说我不能使用原子矢量$
Unt_Richness_anova <- aov(Unt$richness, TreatmentDay)
所以,我觉得很困惑我可以在一个实例中使用原子向量中的$运算符但不能用于另一个实例。 有人可以帮忙吗?
答案 0 :(得分:0)
通过查看aov()
函数的源代码,它实际上是对另一个名为terms()
的函数的调用,导致此错误。
现在,如果在terms()
函数中未指定数据框时调用aov()
函数,则terms()
函数似乎只会采用公式从aov()
函数传递的样式参数。
请注意,以下代码不起作用(如您所指定):
Unt_Richness_anova <- aov(Unt$richness, Unt$TreatmentDay)
如果将两个变量表示为公式,那么一切都按预期工作:
Unt_Richness_anova <- aov(Unt$richness ~ Unt$TreatmentDay)
问题不在于$
运算符,而在于terms()
函数。 terms()
函数似乎只能处理单独的变量,如果它们属于同一数据框 AND ,则在aov()
函数中指定数据框。
为什么会出现这种情况我无法回答,但希望其他人可以解释一下?