首页 \ 问答 \ 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.

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的