【发布时间】:2020-04-24 15:28:46
【问题描述】:
我正在尝试使用 for 循环计算中国和美国位置的总价:
请参考以下示例数据框和代码:
z <- data.frame(location = c("china", "china", "US", "US" ), quantity = c(100, 200, 100, 200))
## Calculate Total price, considering price for one quanity is $1 for china and $3 for US
for row in 1:nrow(z) {
l <- z[row, "location"]
q <- z[row, "quanity"]
ifelse (l == "china",
z$total <- (z$quantity * 1),
z$total <- (z$quantity * 3))
【问题讨论】: