是否有 R 函数将数据框中包含向量的两列相乘?

Is there an R function to multiply two columns contain vectors in a data frame together?

我有两个数据框,我被要求计算折扣前的小计。我尝试将价格列与第一个数据框中的数量列相乘,然后使用 R 代码

添加第二个数据框中的运费
Order <- transform(
  Order,
  Sub_Total_Before_Discount=((raw$Prices * raw$Quantities) + Order$Delivery_Fee)
)

它returns错误

Error in raw$Prices * raw$Quantities : non-numeric argument to binary operator

我还是 R 的新手,如果有什么方法可以计算这个,请帮助我。非常感谢。

列绑定两个数据框

df = cbind(dataframe1, dataframe2)

将两列都更改为数字

raw$Prices = as.numeric(unlist(raw$Prices))
raw$Quantities = as.numeric(unlist(raw$Quantities))
Order$Delivery_Fee = as.numeric(unlist(Order$Delivery_Fee))

用 dplyr 乘以列

library(dplyr)
df <- df %>% mutate(order = Prices * Quantities)

最终结果

df$final_price <- df$order + df$Delivery_Fee