我对 f 语句有疑问,因为它返回给我以下错误消息:“条件长度 > 1 并且仅使用第一个元素”我有一个名为 data.summary 的数据框,我想创建两个新变量vol.up and 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.
}
我真的不明白为什么 if 语句不适用于向量?
预先感谢
您的比较是一个具有多个元素的 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)