【问题标题】:Alternative to if statements inside for loops in R替代 R 中 for 循环中的 if 语句
【发布时间】:2021-03-06 12:41:15
【问题描述】:

我有一个包含超过 400'000 行的数据框。我正在尝试按以下方式组织我的数据:

for (i in 1:nrow(full_data)) {
    if (full_data$amount0In[i] > full_data$amount0Out[i]) {
      full_data$amount0[i] <- full_data$amount0In[i]
      full_data$amount1[i] <- -full_data$amount1Out[i]
    } else {
      full_data$amount0[i] <- -full_data$amount0Out[i]
      full_data$amount1[i] <- full_data$amount1In[i]
    }
  }

此代码有效,但计算时间很长。是否有任何替代方案(例如矢量化)可以使这段代码更高效?

【问题讨论】:

  • 可能 - 你能描述一下你的代码在做什么吗?此外,看起来只有 1 个循环 - 嵌套循环是指在另一个循环中有一个循环。
  • 不要使用循环。直接处理数据框中的列。 StackOverflow 上有很多很多很多问题,网络上的教程会告诉你该怎么做。
  • 我找不到这方面的具体例子。您能否分享一个很好的资源来解释这一点?
  • 这是一个针对新 R 用户的blog post on vectorization。或下载The R Inferno的免费PDF。

标签: r loops vectorization


【解决方案1】:

使用 R 的矢量化,您可以做到这一点 - 这相当于您的代码,但会更快,因为它是矢量化的。

In_gt_Out <- full_data$amount0In > full_data$amount0Out
full_data$amount0[In_gt_Out] <-  full_data$amount0In[In_gt_Out]
full_data$amount1[In_gt_Out] <- -full_data$amount1Out[In_gt_Out]

full_data$amount0[!In_gt_Out] <- -full_data$amount0Out[!In_gt_Out]
full_data$amount1[!In_gt_Out] <-  full_data$amount1In[!In_gt_Out]

pminpmax 可能有更简单的方法,但没有任何示例数据或目标描述,很难确定发生了什么。

【讨论】:

  • 我有 4 列,“amount0in”、“amount0Out”、“amount1In”和“amount1Out”,它们代表交易,例如2 个 amount0In 和 4 个 amount1Out 将代表 2 个 amount1 代币与 4 个 amount2 代币的交易。目标是将它们分为两列“amount0”(在本例中 = 2)和“amount1”(在本例中 = -4),流出为负值,流入为正值。希望这更清楚!
【解决方案2】:

也许这会对你有所帮助:

library(dplyr)
full_data <- full_data %>% mutate(
      amount0 = if_else(amount0In > amount0Out, amount0In,amount0Out),
      amount1 = if_else(amount1In > amount1Out, amount1In,amount1Out)
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 2020-05-25
    • 1970-01-01
    • 2016-06-16
    • 2019-06-09
    相关资源
    最近更新 更多