Matheus Monteiro
Matheus Monteiro

Reputation: 11

Sum values from dataframe considering two column interval

Want to sum values from two column considering the intervals and overlaps.

Sample example:

df = data.frame(A = c(1, 1, 2), B = c(1, 600, 600), C = c(0.02, 0.03, 0.01))

Rules for summarization:

For each column (A & B) the union are corresponding an index value for an individual interval. That I need to sum to create vector that I will use to perform financial projection / valuation. In the end, I would like to retrieve the overlaps inclued

for example:

1 to 600 - 0.03
2 to 600 - 0.01

From the value 2 to 600 i'm also including the interval 1 to 600, so I need to sum considering this overlap.

Expected output of the sum:

### 1 - 0.05
### 2 - 0.04
### 600 - 0.04

I think now I could explain everything:

enter image description here

Upvotes: 0

Views: 54

Answers (1)

Maurits Evers
Maurits Evers

Reputation: 50668

You're leaving us guessing somewhat as to what the rules are for reproducing your expected output.

I can't reproduce your output but is this what you're after?

library(dplyr)
library(tidyr)
df %>% 
    pivot_longer(-C, values_to = "id") %>% 
    select(-name) %>% 
    group_by(id) %>% 
    summarise(value = sum(C))
## A tibble: 3 x 2
#     id value
#  <dbl> <dbl>
#1     1  0.07
#2     2  0.01
#3   600  0.04

Upvotes: 1

Related Questions