-1

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....

Joe Ng
  • 51
  • 6
  • Please fix your code, it doesn't work. – Ronak Shah Oct 01 '21 at 12:11
  • i just want to get new column where order * Prices. For example: a*P1 = 1*12+2*13+3*4 =50 – Joe Ng Oct 01 '21 at 12:22
  • Please update the question with a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). – dario Oct 01 '21 at 12:32
  • Post the output of `dput(your_dataframe)` into your question. – user2974951 Oct 01 '21 at 12:33
  • I have upload the image of my dataframe as the data is given so I am not sure how to really code it – Joe Ng Oct 01 '21 at 12:55
  • Please do not post photos of data or code! If you do, people who are willing to help you would have to type out all that text. Instead proved a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) P.S. Here is [a good overview on how to ask a good question](https://stackoverflow.com/help/how-to-ask) – dario Oct 01 '21 at 12:56
  • Thank you so much! I have updated my question. Pls double check – Joe Ng Oct 01 '21 at 13:04

3 Answers3

0

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.

0
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)

danlooo
  • 10,067
  • 2
  • 8
  • 22
0

Here is an option in base R

d1 <- data.frame(Order = I(list(a, b, c)), Price = I(list(P1, P2, P3)))
d1$Sub_total <- unlist(Map(`%*%`, d1$Order, d1$Price))

-output

> d1
    Order     Price Sub_total
1 1, 2, 3 12, 13, 4        50
2    1, 2      2, 4        10
3    3, 1     12, 1        37
akrun
  • 874,273
  • 37
  • 540
  • 662