簡體   English   中英

在R中創建條件軸標簽?

[英]Creating conditional axis labels in R?

我想知道為什么在運行以下R函數時收到以下警告:

Warning message: In if (is.na(labels)) axTicks(2) else labels : the condition has length > 1 and only the first element will be used

由於除此警告消息外,其他一切正常,我想知道如何消除此警告消息?

bb <- function(labels = NA){

 plot(1, yaxt = "n")

 lab <- if(is.na(labels)) axTicks(2) else labels ## Why this gives a warning message?

 axis(2, at = axTicks(2), labels = lab)
}
# Example of use:
bb(labels = paste0("Hi ", 1:5))

假設labels是向量,則is.na(labels)將返回TRUE / FALSE值的向量。 在R中,標准if語句必須計算為單個 TRUE / FALSE值。 警告消息告訴您if產生了多個值,並且正在根據第一個值評估真相。

如果您的目標是檢查labels中是否有任何NA ,請執行以下操作:

if (any(is.na(labels))) {
  ...
}

如果您的目標是用另一個向量的值替換某個向量的NA元素,則可能需要ifelse()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM