Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

In R, how to sum certain rows of a data frame with certain logic?

$
0
0

Hi experienced R users,

It's kind of a simple thing. I want to sum x by Group.1 depending on one controllable variable.

I'd like to sum x by grouping the first two rows when I say something like: number <- 2 If I say 3, it should sum x of the first three rows by Group.1

Any idea how I might tackle this problem? Should I write a function? Thank y'all in advance.

  Group.1  Group.2      x
1       1     Eggs 230299
2       2     Eggs 263066
3       3     Eggs 266504
4       4     Eggs 177196

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>