簡體   English   中英

是否有 R 函數將數據框中包含向量的兩列相乘?

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

我有兩個數據框,我被要求在折扣前計算小計。 我嘗試將 Prices 列與第一個數據框中的 Quantities 列相乘,然后使用 R 代碼從第二個數據框中添加 Delivery Fee

Order <- transform(
  Order,
  Sub_Total_Before_Discount=((raw$Prices * raw$Quantities) + Order$Delivery_Fee)
)

它返回錯誤

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM