R:使用rollapply对列组进行求和的问题(R: Problems using rollapply to sum groups of columns)
我应该是一个简单的问题,但我对R很新,所以它让我感到难过。
我有一系列25列代表行为实验中的试验。 我想使用roll apply来汇总前5列,然后是6-10,11-15,依此类推,这样我最终得到一个包含输出的新的5列数据框(类似于5列示例)下面)。 真的,重点是能够快速更改“bin大小”,以便我可以决定哪种“分辨率”最适合数据。 最后我不会只是总结,但我认为这个答案将足以让我滚动。
INPUT: Col1 Col2 Col3 Col4 Col5 1 1 1 1 1 2 2 2 2 2 DESIRED OUTPUT: Col1 5 10
I have what should be a simple question, but I'm very new to R so it's stumping me.
I have a series of 25 columns representing trials in a behavioral experiment. I would like to use roll apply to sum the first 5 columns, then 6-10, 11-15, and so on, so that I end up with a new, 5 column data frame containing the output (similar to the 5 column sample below). Really, the point is to be able to rapidly change the "bin size" so that I can make decisions about what "resolution" best suits the data. In the end I wont just be summing, but I think this answer would be plenty to get me rolling.
INPUT: Col1 Col2 Col3 Col4 Col5 1 1 1 1 1 2 2 2 2 2 DESIRED OUTPUT: Col1 5 10
原文:https://stackoverflow.com/questions/17243565
最满意答案
你应该只使用RTTI而不是重新发明轮子。
如果您坚持不使用RTTI,则可以使用CRTP和函数本地静态变量来避免必须将函数写入每个派生类。 改编自我为维基百科撰写的示例代码: http : //en.wikipedia.org/wiki/Curiously_recurring_template_pattern#Polymorphic_copy_construction
另一个替代方法是读取vtable指针(通过
this
和指针算术),但这取决于编译器和平台,因此它不可移植。You should just use RTTI instead of reinventing the wheel.
If you insist on not using RTTI, you could use CRTP and a function-local static variable to avoid having to write the function to every derived class. Adapt from this example code I wrote for Wikipedia: http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern#Polymorphic_copy_construction
Another alternative is reading the vtable pointer (via
this
and pointer arithmetics), but that would depend on both the compiler and the platform, so it is not portable.
相关问答
更多-
我可以避免RTTI吗?(Can I avoid RTTI?)[2023-08-21]
这可以通过使用双重调度来完成 class Number { protected: virtual Number& operator+(const Integer&) = 0; virtual Number& operator+(const Complex&) = 0; virtual Number& operator-(const Integer&) = 0; virtual Number& operator-(const Complex&) = 0; // For all inheri ... -
constexpr和RTTI(constexpr and RTTI)[2023-07-16]
你认为运行时类型标识的哪些部分在编译时起作用? 常量表达式的规则不允许: - 一个typeid表达式(5.2.8),其操作数是一个多态类类型的glvalue; 所以你的模板只适用于某些类型。 RTTI关闭后,根本无法使用typeid 。 C ++ 11已经提供了一种散列类型的机制: return ::std::hash<::std::type_index>()(::std::type_index(typeid(T))); 但它不会是所有类型的常量表达式。 您可以使用指向每种类型的指针的type_index ... -
不,泛型完全是编译时。 No, generics are entirely compiletime.
-
Delphi中的RTTI仍然不像.NET或其他托管语言中的Reflection那样全面,因为它在编译的代码上运行,而不是中间语言(字节码)。 然而,这是一个非常相似的概念,Delphi 2010中的新的RTTI系统使其更加接近于反思,揭示了整个面向对象的API。 在D2010之前,RTTI非常有限。 关于我以前记住做的唯一的事情是将枚举类型转换为字符串 ( 反之亦然 ),以供下拉列表使用。 我可能会一直使用它来控制持久性 。 随着新的RTTI在D2010,你可以做更多的事情: XML序列化 基于属性的元数据 ...
-
boost :: any需要RTTI吗?(Does boost::any need RTTI?)[2022-03-23]
因为boost boost::any不需要boost 1.57 RTTI。 记住所有用作boost::any对象boost::any必须是可复制的。 https://svn.boost.org/trac/boost/ticket/10346 Since boost 1.57 RTTI is not needed for boost::any. Rememeber that all objects used as boost::any must by copyable. https://svn.boost.o ... -
你应该只使用RTTI而不是重新发明轮子。 如果您坚持不使用RTTI,则可以使用CRTP和函数本地静态变量来避免必须将函数写入每个派生类。 改编自我为维基百科撰写的示例代码: http : //en.wikipedia.org/wiki/Curiously_recurring_template_pattern#Polymorphic_copy_construction 另一个替代方法是读取vtable指针(通过this和指针算术),但这取决于编译器和平台,因此它不可移植。 You should just us ...
-
你在这里问三个不同的问题。 最初的问题是询问是否有任何方法可以让MSVC不生成名称,或者是否可以与其他编译器一起使用,或者,如果没有这样做,是否有任何方法可以从生成的type_info中删除名称而不会破坏名称。 然后你想知道是否有可能修改MS ABI(可能不是太彻底),以便可以剥离名称。 最后,您想知道是否可以设计没有名称的ABI。 问题#1本身就是一个复杂的问题。 据我所知,没有办法让MSVC不生成名称。 大多数其他编译器都针对专门定义typeid(foo).name()必须返回的ABI,因此它们也不能生 ...
-
你的错误是TRttiArrayType用于静态数组(并且你的数组是动态的),修复问题使用TRttiDynamicArrayType如下所示: Writeln(TRttiDynamicArrayType(T).ElementType.Name); You cast is wrong the TRttiArrayType is for static arrays (and your array is dynamic), to fix the issue use the TRttiDynamicArrayTy ...
-
在最新的修补程序中似乎有此问题的错误。 我已经向开发团队报告了这个问题,应该很快修复。 我注意到自己在不同版本的SMS上测试RTTI方法完全相同。 立即解决方案是回滚到版本2.0.0.723。 您可以在此处下载该版本: http : //smartmobilestudio.com/download/v2_0_0_723/ There seem to be a bug with this issue in the latest hotfix. I have reported the issue to the ...
-
Qt RTTI麻烦(Qt RTTI trouble)[2022-03-09]
使用qobject_cast和/或obj->metaObject()->className()代替。 Use qobject_cast and/or obj->metaObject()->className() instead.