Re: [R] Create new data frame with conditional sums

2023-10-14 Thread Jeff Newmiller via R-help
Pre-compute the per-interval answers and use findInterval to look up the per-row answers... dat <- read.table( text= "Tract Pct Totpop 1 0.054000 2 0.033500 3 0.014500 4 0.124100 5

Re: [R] Create new data frame with conditional sums

2023-10-14 Thread Bert Gunter
Well, here's one way to do it: (dat is your example data frame) Cutoff <- seq(0, .15, .01) Pop <- with(dat, sapply(Cutoff, \(p)sum(Totpop[Pct >= p]))) I think there must be a more efficient way to do it with cumsum(), though. Cheers, Bert On Sat, Oct 14, 2023 at 12:53 AM Jason Stout, M.D.

[R] Create new data frame with conditional sums

2023-10-14 Thread Jason Stout, M.D.
This seems like it should be simple but I can't get it to work properly. I'm starting with a data frame like this: Tract Pct Totpop 1 0.054000 2 0.033500 3 0.014500 4 0.124100 5 0.21