【发布时间】:2017-10-10 00:36:47
【问题描述】:
使用循环和条件语句,我想识别值超过 2.50 的行
customer <- c("john", "amy", "doug")
product <- c("coffee", "pastry", "pastry")
store <- c("a", "b", "c")
cost <- c(5.50, 2.45, 3.00)
df <- data.frame(customer, product, store, cost)
我想识别超过 2.50 美元的购买并将“商店”和“产品”保存为与超过 2.50 美元的购买相关联的单独向量。
到目前为止,这是我的代码,但它不工作......
for (row in 1:nrow(df)) {
customer <- df[row, "customer"]
product <- df[row, "product"]
store <- df[row, "store"]
cost <- df[row, "cost"]
if(cost > 2.50) {
print(paste(customer, "purchased", product, "at", store, "for", cost))
}
}
这不是工作,我如何将两个“产品”和“商店”保存为单独的向量?
【问题讨论】:
-
为什么需要保存它们?
标签: r