所以我的任务是基本上要得到下面的图。数据来自一个数据框,其中有一些值被标记为NA.因此,我的代码如下。
> plot(temp,ozone,
+ xlab = "temperature",
+ ylab = "ozone",
+ col = ifelse(which(ozone>100), "red", "orange",),
+ pch = 17)
但是我现在得到了一个错误。
"Error in ifelse(which(ozone > 100), "red", "orange", ) :
unused argument (alist())
如果有任何反馈意见,我将感激不尽 I would appreciate any feedbackpointers on what has gone wrong. 我之前也试过这样的方法。
highlevels <- which(ozone>100)
lowlevels <- which(ozone<100)
col = c("red","orange")[highlevels,lowlevels]
删去 which
再裹上 ifelse
是一种选择。
plot(temp, ozone,
xlab = "temperature",
ylab = "ozone",
col = ifelse(ifelse(!is.na(ozone), ozone, 0) > 100, "red", "orange"),
pch = 17)
否则,像这样的功能 tidyr::replace_na
或 dplyr::coalesce
如果你正在使用这些库,可以帮助你。
另一种选择是将所有变量子集为那些非缺失的变量。
还有一种方法是设置
col = c("red", "orange")[(ozone > 100) + 1]
但这是有点棘手。