我是R的新手。我试图根据if else语句为列分配一些值。但它一直让我犯错误。有人可以帮我理解错误是什么吗?
代码:
if (REAR[mean < (-2) & mean > 2,]) {
REAR$EXCEPTION_1<-1; REAR$EXCEPTION_2<-0
} else if(REAR[!(mean < (-2) & mean > 2) & deviation > 1.5,]) {
REAR$EXCEPTION_1<-0; REAR$EXCEPTION_2<-1
} else {
REAR$EXCEPTION_1<-0; REAR$EXCEPTION_2<-0
}
Error:
Error:
Error in mean < (-5) :
comparison (3) is possible only for atomic and list types
示例数据:
dput(head(REAR))
structure(list(TIMESTAMP = structure(list(c(0, 0, 0,
0, 0, 0), c(45L, 0L, 15L, 30L, 45L, 0L), c(17L, 18L, 18L, 18L,
18L, 19L), c(5L, 5L, 5L, 5L, 5L, 5L), c(7L, 7L, 7L, 7L, 7L, 7L
), c(114L, 114L, 114L, 114L, 114L, 114L), c(2L, 2L, 2L, 2L, 2L,
2L), c(216L, 216L, 216L, 216L, 216L, 216L), c(0L, 0L, 0L, 0L,
0L, 0L), c("IST", "IST", "IST", "IST", "IST", "IST"), c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
)), class = c("POSIXlt", "POSIXt")), ID = c(28808, 28808,
28808, 28808, 28808, 28808), CONNECTION= c("UP", "UP",
"UP", "UP", "UP", "UP"), ACTIVE = c(68.814, 68.814,
68.814, 68.814, 68.814, 68.814), SET = c(70,
70, 70, 70, 70, 70), MODE = c("Cool", "Cool", "Cool",
"Cool", "Cool", "Cool"), STATUS = c("Occupied", "Occupied",
"Occupied", "Occupied", "Occupied", "Occupied"), KNOB= c("On",
"On", "On", "On", "On", "On"), loopN = c(140805L, 140805L, 140805L,
140805L, 140805L, 140805L), timediff = c(15, 15, 15, 15, 15,
15), diff= c(-1.18600000000001, -1.18600000000001,
-1.18600000000001, -1.18600000000001, -1.18600000000001, -1.18600000000001
), mean = c(-1.67920000000001, -1.67920000000001, -1.67920000000001,
-1.67920000000001, -1.67920000000001, -1.67920000000001), deviation= c(0.553944943112579,
0.553944943112579, 0.553944943112579, 0.553944943112579, 0.553944943112579,
0.553944943112579), EXCEPTION_1 = c(0, 0, 0, 0, 0,
0), EXCEPTION_2 = c(0, 0, 0, 0, 0, 0)), .Names = c("TIMESTAMP",
"ID", "CONNECTION", "ACTIVE ", "SET",
"MODE", "STATUS", "KNOB",
"loopN", "timediff", "diff", "mean", "deviation",
"EXCEPTION_1", "EXCEPTION_2"), row.names = 764:769, class = "data.frame")
答案 0 :(得分:2)
你的错误来自&#34; [&#34;功能到&#34;知道&#34;您打算使用&#39;表示&#39;作为列名。它能够找到的唯一mean
是函数均值,并且尝试与数字进行比较会抛出您得到的特定错误。 (通常使用令牌命名列也是不好的做法,这些令牌也是常用函数的名称,但这不是错误的原因。如果有一个名为mean
的变量,解释器会已经使用了它的值。除非你使用with
或使用[[name]]
或其他棘手的设备(如eval
或{{1)公开它们,否则列名称不会对运营商显示#34;允许指定&#34;评估环境&#34;)
替代(但可能不是一个成功的):
substitute
但有人指出,这并没有解决假设R是SAS(或SPSS)的进一步困难。在R中,当使用if ( with(REAR , mean < (-2) & mean > 2) ) {
REAR$EXCEPTION_1<-1; REAR$EXCEPTION_2<-0
} else { if( with( REAR, !( mean < (-2) & mean > 2) &
standardeviation > 1.5)
) { REAR$EXCEPTION_1<-0; REAR$EXCEPTION_2<-1
} else { REAR$EXCEPTION_1<-0; SAT$EXCEPTION_2<-0
}
}
- 构造时,没有沿着列向量的隐式循环。 if(.){.}else{.}
可用于在逻辑前因向量的基础上在两个(可能是计算的)值向量之间进行选择。在任何情况下你都需要学习R的矢量化方法第一个条件可以通过将一个转换为布尔0/1数值的逻辑矢量乘以1来处理:
ifelse
等一下!它不可能满足这种条件。在构造逻辑表达式时,您是否对AND和OR的含义感到困惑?不应该是???? :
REAR$EXCEPTION_1 <- with(REAR , mean < (-2) & mean > 2)*1 # *1 makes numeric
第二个结果可能是:
REAR$EXCEPTION_1 <- with(REAR , mean < (-2) | mean > 2)*1
您不需要最后的REAR$EXCEPTION_2 <- with(REAR, !(mean < (-2) | mean > 2) & deviation > 1.5)*1
子句,因为前两个分配将所有其他条件设置为零。我假设分配到SAT $ EXCEPTION_2是另一个错误,你还没有修复。
如果您使用}else{}
,则需要了解无法进行分配,当然也不能分配不同的向量。