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

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

问题描述

我想一次添加一行数据框的列的总和.

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 和 apply(或 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天全站免登陆