Susan Mwansa
Susan Mwansa

Reputation: 51

How to get the product of two columns in R

So i'm trying to get the product of 2 columns in a data frame and I keep getting errors. Can anyone help?

Please see below

dput(PQ[1:10, "Prices", drop = FALSE])
structure(list(Prices = list(13.99, c(13.95, 14.95), c(13.99, 
10.99), c(10.99, 13.95, 13.99), c(14.95, 11.95, 13.99), c(11.95, 
13.95), c(11.95, 12.95), c(13.99, 14.95, 10.99, 11.95), 12.95, 
    c(10.99, 13.95))), row.names = c(NA, 10L), class = "data.frame")

dput(PQ[1:10, "Quantities", drop = FALSE])
structure(list(Quantities = list(2L, c(1L, 1L), c(3L, 3L), c(2L, 
2L, 3L), 1:3, 2:1, c(3L, 1L), c(3L, 1L, 2L, 2L), 1L, c(3L, 1L
))), row.names = c(NA, 10L), class = "data.frame")

I tried

PQ <- RawOrders[,c("Prices", "Quantities")]

PQ <- PQ %>% mutate(Sub_Total = Prices * Quantities)

but i kept getting the following error message -> Error in Prices * Quantities : non-numeric argument to binary operator

Upvotes: 2

Views: 71

Answers (3)

priya
priya

Reputation: 105

yOU CAN TRY THIS

PQ$new = mapply(Prices, Quantities, ~ .x * .y))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388817

In base R, you can use mapply :

PQ$Sub_Total <- mapply(function(x, y) sum(x * y), PQ$Prices, PQ$Quantities)
PQ

# A tibble: 10 x 3
#   Prices    Quantities Sub_Total
#   <list>    <list>         <dbl>
# 1 <dbl [1]> <int [1]>       28.0
# 2 <dbl [2]> <int [2]>       28.9
# 3 <dbl [2]> <int [2]>       74.9
# 4 <dbl [3]> <int [3]>       91.8
# 5 <dbl [3]> <int [3]>       80.8
# 6 <dbl [2]> <int [2]>       37.8
# 7 <dbl [2]> <int [2]>       48.8
# 8 <dbl [4]> <int [4]>      103. 
# 9 <dbl [1]> <int [1]>       13.0
#10 <dbl [2]> <int [2]>       46.9

Upvotes: 0

akrun
akrun

Reputation: 886948

The column are list columns. We can use map2 from purrr to loop over the list and do the multiplication

library(dplyr)
library(purrr)
PQ %>%
     mutate(Sub_Total = map2(Prices, Quantities, ~ .x * .y))

-output

# A tibble: 10 x 3
#   Prices    Quantities Sub_Total
#   <list>    <list>     <list>   
# 1 <dbl [1]> <int [1]>  <dbl [1]>
# 2 <dbl [2]> <int [2]>  <dbl [2]>
# 3 <dbl [2]> <int [2]>  <dbl [2]>
# 4 <dbl [3]> <int [3]>  <dbl [3]>
# 5 <dbl [3]> <int [3]>  <dbl [3]>
# 6 <dbl [2]> <int [2]>  <dbl [2]>
# 7 <dbl [2]> <int [2]>  <dbl [2]>
# 8 <dbl [4]> <int [4]>  <dbl [4]>
# 9 <dbl [1]> <int [1]>  <dbl [1]>
#10 <dbl [2]> <int [2]>  <dbl [2]>

If we want to get the sum

PQ %>%
     mutate(Sub_Total = map2_dbl(Prices, Quantities,
           ~ sum(.x * .y, na.rm = TRUE)))

-output

# A tibble: 10 x 3
#   Prices    Quantities Sub_Total
#   <list>    <list>         <dbl>
# 1 <dbl [1]> <int [1]>       28.0
# 2 <dbl [2]> <int [2]>       28.9
# 3 <dbl [2]> <int [2]>       74.9
# 4 <dbl [3]> <int [3]>       91.8
# 5 <dbl [3]> <int [3]>       80.8
# 6 <dbl [2]> <int [2]>       37.8
# 7 <dbl [2]> <int [2]>       48.8
# 8 <dbl [4]> <int [4]>      103. 
# 9 <dbl [1]> <int [1]>       13.0
#10 <dbl [2]> <int [2]>       46.9

data

PQ <- structure(list(Prices = list(13.99, c(13.95, 14.95), c(13.99, 
10.99), c(10.99, 13.95, 13.99), c(14.95, 11.95, 13.99), c(11.95, 
13.95), c(11.95, 12.95), c(13.99, 14.95, 10.99, 11.95), 12.95, 
    c(10.99, 13.95)), Quantities = list(2L, c(1L, 1L), c(3L, 
3L), c(2L, 2L, 3L), 1:3, 2:1, c(3L, 1L), c(3L, 1L, 2L, 2L), 1L, 
    c(3L, 1L))), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

Upvotes: 1

Related Questions