首页 \ 问答 \ R:使用rollapply对列组进行求和的问题(R: Problems using rollapply to sum groups of columns)

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
更新时间:2024-05-04 20:05

最满意答案

你应该只使用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.

相关问答

更多
  • 这可以通过使用双重调度来完成 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 ...
  • 你认为运行时类型标识的哪些部分在编译时起作用? 常量表达式的规则不允许: - 一个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 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 ...
  • 使用qobject_cast和/或obj->metaObject()->className()代替。 Use qobject_cast and/or obj->metaObject()->className() instead.

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。