“条件的长度为>在图1中,将仅使用第一元素.错误 [英] "the condition has length > 1 and only the first element will be used" error

查看:44
本文介绍了“条件的长度为>在图1中,将仅使用第一元素.错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对f语句有问题,因为它返回了以下错误消息:条件的长度> 1,并且仅将使用第一个元素" 我有一个名为data.summary的数据框,我想根据数据框的另一个变量创建两个新变量 vol.up vol.down .这是我的脚本代码:

I have a problem with the f statement, because it's returning to me this error message : "the condition has length > 1 and only the first element will be used" I have a dataframe called data.summary, and I want to create two new variable vol.up and vol.down depending on the other variable of my dataframe. This is my script code :

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不起作用?预先感谢

I don't really inderstand why f statement doesn't work for vector ? Thank in advance

推荐答案

对于 if - else ,您的比较是一个包含多个元素的TRUE/FALSE向量要正常工作,通常必须使用长度不是 NA 的长度为1的逻辑矢量.也许您需要对条件进行矢量化处理

Your comparison is a TRUE/FALSE vector with more than one element, for the if - else to work it is usually necessary a length-one logical vector that is not NA. Maybe what you need is to vectorize the conditions:

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 

这篇关于“条件的长度为&gt;在图1中,将仅使用第一元素.错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆