比较R中的两个整数:“较长对象长度不是较短对象长度的倍数”德普利

发布于 2024-12-21 20:24:16 字数 1176 浏览 3 评论 0原文

当比较两个整数以在用户定义函数中对数据帧进行子集化时,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2024-12-28 20:24:16

ddply 函数已通过 validSession 对数据帧进行子集化。因此,仅向 transform 提供一个包含与特定 validSession 对应的所有行的数据帧。

也就是说,transform 已经被馈送到 unique(mediandf$validSession) 中的每个 ssubset(mediandf,validSession==s)

由于您不必执行任何子集化(ddply 会处理这一点),因此您需要做的就是:

ddply(mediandf,.(validSession),transform,grossMed2=median(absStudentDeviation))

然后您将使用 mediandf 返回新列 grossMed2 包含您想要的值(因此每个唯一的 validSession 中的值都相同)。

The ddply function already subsets your data frame by validSession. Hence transform is only fed a data frame with all the rows corresponding to a particular validSession.

That is, transform is already being fed subset(mediandf,validSession==s) for each s in unique(mediandf$validSession).

Since you don't have to do any subsetting (ddply takes care of that), all you need to do is:

ddply(mediandf,.(validSession),transform,grossMed2=median(absStudentDeviation))

And then you'll get mediandf back out with a new column grossMed2 with the value you want (so it will be the same value within each unique validSession).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文