比较R中的两个整数:“较长对象长度不是较短对象长度的倍数”德普利
当比较两个整数以在用户定义函数中对数据帧进行子集化时,我在 R 中收到“较长对象长度不是较短对象长度的倍数”警告。
用户定义的函数只返回从数据帧中获取的整数子集的中位数:(
function(s){
return(median((subset(EDB,as.integer(validSession) == as.integer(s)))$absStudentDeviation))
}
我最初在那里没有 as.integer
强制。我把它们放在那里进行调试、文本,然后我我仍然收到错误。)
我收到的具体错误是:
在 as.integer(validSession) == as.integer(s) 中: 较长的对象长度不是较短对象长度的倍数
我在调用时收到此警告超过 50 次:
mediandf <- ddply(mediandf,.(validSession),
transform,
grossMed2 = medianfuncEDB(as.integer(validSession)))
目标是计算与大型数据帧 EDB 中给定 validSession 关联的 $validSession 的中位数,并将该向量附加到 midandf。
实际上,我已经通过使用 is.integer(validSession)
进行子集化,仔细检查了 Mediadf 数据帧和 EDB 数据帧中 validSession 的所有值都是整数。
此外,看来该命令实际上执行了我的意图,我在数据框中得到了一个新列,其中包含我尚未验证的值,但我想了解该警告。如果使用整数作为输入来调用“medianfuncEDB”,为什么在调用 s == validSession
时我会得到“较长的对象长度不是较短的对象长度的倍数”?
请注意,简单的函数调用(例如 medianfuncEDB(5)
)可以正常工作,不会出现任何问题,那么为什么我在使用 ddply 时会收到警告?
编辑:我在乔兰评论的帮助下发现了问题。我不知道转换将整个矢量输入到函数中。使用 validSession[1] 则不会发出任何警告。
I'm getting an "longer object length not multiple of shorter object length" warning in R when comparing two integers to subset a dataframe in the midst of a user defined function.
The user defined function just returns the median of a subset of integers taken from a dataframe:
function(s){
return(median((subset(EDB,as.integer(validSession) == as.integer(s)))$absStudentDeviation))
}
(I did not originally have the as.integer
coercions in there. I put them there to debug, text, and I'm still getting an error.)
The specific error I'm getting is:
In as.integer(validSession) == as.integer(s) :
longer object length is not a multiple of shorter object length
I get this warning over 50 times when calling:
mediandf <- ddply(mediandf,.(validSession),
transform,
grossMed2 = medianfuncEDB(as.integer(validSession)))
The goal is to calculate the median of $validSession associated with the given validSession in the large dataframe EDB and attach that vector to mediandf.
I have actually double-checked that all values for validSession in both the mediandf dataframe and the EDB dataframe are integers by subsetting with is.integer(validSession)
.
Furthermore, it appears that the command actually does what I intend, I get a new column in my dataframe with values I have not verified, but I want to understand the warning. if "medianfuncEDB" is being called with an integer as its input, why am I getting a "longer object length is not multiple of shorter object length" when s == validSession
is called?
Note that simple function calls, like medianfuncEDB(5)
work without any problems, so why do I get warnings when using ddply?
EDIT: I found the problem with the help of Joran's comment. I did not know that transform fed entire vecotrs into the function. Using validSession[1] instead gave no warnings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ddply
函数已通过validSession
对数据帧进行子集化。因此,仅向transform
提供一个包含与特定validSession
对应的所有行的数据帧。也就是说,
transform
已经被馈送到unique(mediandf$validSession) 中的每个
。s
的subset(mediandf,validSession==s)
)由于您不必执行任何子集化(
ddply
会处理这一点),因此您需要做的就是:然后您将使用
mediandf
返回新列grossMed2
包含您想要的值(因此每个唯一的validSession
中的值都相同)。The
ddply
function already subsets your data frame byvalidSession
. Hencetransform
is only fed a data frame with all the rows corresponding to a particularvalidSession
.That is,
transform
is already being fedsubset(mediandf,validSession==s)
for eachs
inunique(mediandf$validSession)
.Since you don't have to do any subsetting (
ddply
takes care of that), all you need to do is:And then you'll get
mediandf
back out with a new columngrossMed2
with the value you want (so it will be the same value within each uniquevalidSession
).