Home > Net >  Sum Product of a list in dataframe by Order id in R
Sum Product of a list in dataframe by Order id in R

Time:10-02

I am doing a project which is similar to Uber Eat. I want to create new column in a data frame to calculate sub-total of these orders but because the class of each column is "list", R is not allowing me to do that. Do you know any ways to do it.

Thank you

a = c(1,2,3)
b = 1:2
c = (3,1)


P1 = c(12,13,4)
P2 = c(2,4)
P3 = c(12,1)

#My given dataframe will be:
Order  | Price   | Sub-total
a      | P1      |   sum(a*P1)
b      | P2      |   sum(b*P2)
c      |  P3     |    sum(c*P3)


Expect output: Subtotal = [50, 10, 37]

Please see the attached image to understand my dataframe My dataframe

My goal is how to compute aP1, bP2, cP3 and then total sum of aP1....

CodePudding user response:

First, store your respective Order and Price data into a list

a = c(1,2,3)
b = 1:2
c = c(3,1)

P1 = c(12,13,4)
P2 = c(2,4)
P3 = c(12,1)

Order <- list(a, b, c)
Price <- list(P1, P2, P3)

Use a tibble so that you can easily set list columns.

Then using the tidyverse structure, map over the two list columns and apply your formula.

library(dplyr)
library(purrr)

df <- tibble(Order = Order, Price = Price)

df <- df %>% 
  mutate(Sub_total = map2_dbl(Order, Price, ~ sum( .x * .y)))

The result will be as you expected. You can see your original data stored as lists and then your sub-totals.

> df
# A tibble: 3 x 3
  Order     Price     Sub_total
  <list>    <list>        <dbl>
1 <dbl [3]> <dbl [3]>        50
2 <int [2]> <dbl [2]>        10
3 <dbl [2]> <dbl [2]>        37

The total sum would then be sum(df$Sub_total) which is 97.

CodePudding user response:

library(tidyverse)

orders <- list(
  a = c(1,2,3),
  b = 1:2,
  c = c(3,1)
)

prices <- list(
  P1 = c(12,13,4),
  P2 = c(2,4),
  P3 = c(12,1)
)

tibble(
  Order = orders,
  Price = prices
) %>%
  mutate(
    sub_total = Order %>% map2_dbl(Price, ~ sum(.x * .y))
  )
#> # A tibble: 3 x 3
#>   Order        Price        sub_total
#>   <named list> <named list>     <dbl>
#> 1 <dbl [3]>    <dbl [3]>           50
#> 2 <int [2]>    <dbl [2]>           10
#> 3 <dbl [2]>    <dbl [2]>           37

Created on 2021-10-01 by the reprex package (v2.0.1)

  • Related