明智地在数据框行中添加列总和 [英] Adding columns sums in dataframe row wise

查看:91
本文介绍了明智地在数据框行中添加列总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一次将数据框的列总和相加一次.

I would like to add the sums of the columns of my dataframe one row at a time.

因此,对于每一行,我想计算其上方各列的总和.

So for each row, I would like to compute the sum of the columns above it.

是否有结合colSums并应用(或sapply,rollapply)的优雅方法?我一直在尝试将它们组合在一起,但是还不太清楚.

Is there an elegant way to do this with a combination of colSums and apply (or sapply, rollapply)? I have been trying a couple of combinations of those, but could not quite figure it out.

推荐答案

new_df <- apply(data_frame, 2, cumsum)

这篇关于明智地在数据框行中添加列总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆