【问题标题】:Loops and Conditional Statements循环和条件语句
【发布时间】: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


【解决方案1】:

您可以执行以下操作,输出您感兴趣的字符串的向量:

df2 <- df[df$cost > 2.5,]
with(df2, paste(customer, "purchased", product, "at", store, "for", cost))

## [1] "john purchased coffee at a for 5.5" "doug purchased pastry at c for 3"

【讨论】:

  • 谢谢。这行得通!这对于 for 循环和 if 条件语句是否可行?
  • 是的,但是当不使用for 循环时,这些操作通常更高效且更易读。以后有需要的可以看dplyr(其实是tidyverse)和/或data.table;这些非常有效,并且避免了大多数 for 循环的需要。不过,这些对于这个问题不是必需的。
【解决方案2】:

不需要显式的 for 循环。

这会分别保存storeproduct 列,其中cost &gt; 2.5

store.sel <- df[which(df$cost > 2.5), "store"]
product.sel <- df[which(df$cost > 2.5), "product"]

或子集dataframe

subset(df, cost > 2.5)

然后选择所需的列

with(subset(df, cost > 2.5), paste(customer, "purchased", product, "at", store, "for", cost))

【讨论】:

  • 你不需要使用whichdf[df$cost &gt; 2.5, "store"] 给出与df[which(df$cost &gt; 2.5), "store"]相同的结果
  • 是的。但这也不痛。在我看来,返回索引对于更复杂的过滤具有优势,我认为这是一种好的(更好的)实践。参见例如讨论here
【解决方案3】:

我不确定你为什么要保存,但你可以这样做。

   df <- df[df$cost > 2.50,]
   cat(paste0(df$customer, " purchased ", df$product, 
       " at ",df$store, " for ", cost, "./n"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    • 2020-11-21
    • 2021-03-03
    • 2017-03-10
    • 2022-06-21
    • 2022-11-13
    • 2017-03-23
    相关资源
    最近更新 更多