![](/img/trans.png)
[英]Error: condition has length > 1 and only the first element will be used in r
[英]“the condition has length > 1 and only the first element will be used” error
我對f語句有問題,因為它返回給我此錯誤消息: “條件的長度> 1,並且僅將使用第一個元素。”我有一個名為data.summary的數據框,我想創建兩個新的變量vol.up和vol.down取決於我數據框的另一個變量。 這是我的腳本代碼:
data.summary <- call.dat12[,c("Dur...ms.", "Handset.Manufacturer",
"Src.Dst.Sig.Vol..Bytes.", "Dst.Src.Sig.Vol..Bytes.",
"group", "Src.Node.Type", "Dst.Node.Type")]
if (data.summary$Src.Node.Type == "eNodeB"){
data.summary$vol.up <- data.summary$Src.Dst.Sig.Vol..Bytes.
data.summary$vol.down <- data.summary$Dst.Src.Sig.Vol..Bytes.
} else {
data.summary$vol.up <- data.summary$Dst.Src.Sig.Vol..Bytes.
data.summary$vol.down <- data.summary$Src.Dst.Sig.Vol..Bytes.
}
我真的不理解為什么f語句對vector不起作用? 預先感謝
你的比較是與一個以上的元件的TRUE / FALSE矢量,對於if
- else
工作,通常必須不是長度為一個邏輯矢量NA
。 也許您需要對條件進行向量化:
ind <- data.summary$Src.Node.Type == "eNodeB"
data.summary$vol.up<- NA
data.summary$vol.down<- NA
#for true
data.summary$vol.up[ind]<- data.summary$Src.Dst.Sig.Vol..Bytes.[ind]
data.summary$vol.down[ind] <- data.summary$Dst.Src.Sig.Vol..Bytes.[ind]
data.summary
# for false
data.summary$vol.up[!ind]<- data.summary$Dst.Src.Sig.Vol..Bytes.[!ind]
data.summary$vol.down[!ind] <- data.summary$Src.Dst.Sig.Vol..Bytes.[!ind]
data.summary
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.