【问题标题】:Lag of the value, take the previous value logic值的滞后,取之前的值逻辑
【发布时间】:2015-12-15 04:02:59
【问题描述】:

这是我尝试在whatiwant 专栏上实现的目标:

df1 <- data.frame(value = c(99.99,99.98,99.97,99.96,99.95,99.94,
                            99.93,99.92,99.91,99.9,99.9,99.9),
                  new_value = c(NA,NA,99.98,NA,99.97,NA,
                                NA,NA,NA,NA,NA,NA),
                  whatiswant = c(99.99,99.96,99.98,99.95,99.97,99.94,
                                 99.93,99.92,99.91,99.9,99.9,99.9))

用词来解释它 whatiswant 应该有 new_value 的值,对于那些没有 new_value 的词,它应该取下一个可用的最低值。

我认为这是一种滞后的东西。这是data.frame:

   value new_value whatiswant
1  99.99        NA      99.99
2  99.98        NA      99.96
3  99.97     99.98      99.98
4  99.96        NA      99.95
5  99.95     99.97      99.97
6  99.94        NA      99.94
7  99.93        NA      99.93
8  99.92        NA      99.92
9  99.91        NA      99.91
10 99.90        NA      99.90
11 99.90        NA      99.90
12 99.90        NA      99.90

编辑:逻辑解释如下:

  1. 第 1 步。如果 new_value 不是 NA,则 col3 采用该值。所以第三个和 第 5 行已排序。
  2. 第 2 步。第一行 col3 取 col1 的值,因为 col2 为 NA。
  3. Step 3. 2nd row col3 取 col1-row4 的值,作为 2nd 和 3nd col1 的行值已在步骤 1 中使用。
  4. 第四步,第 4 行 col3 取 col1-row5 的值,和上面的所有行一样 来自 col1 是在前面的步骤中获取的。
  5. 第 5 步。col3 中的其余行 6-12 采用相同的值 col1-rows6-12 因为 col2 是 NA 并且数字 col1-row6-12 不是 在前面的步骤中使用。

【问题讨论】:

  • 我也不知道这三列是如何相互关联的......你真的需要在你的问题上付出更多的努力。
  • @N8TRO 看到编辑,现在必须清楚了。请重新打开。
  • @patrick 你同意我们不能填写最后两行吗,因为 9.90 已经在第 10 行使用了?
  • @Tensibai 。不可以,如果所有值都已使用,则默认值应为 col1 的最小值。
  • 好吧,这种情况我就这样适应

标签: r


【解决方案1】:

以函数的形式,每一步在注释中,有不清楚的地方问一下:

t1 <- function(df) {
  df[,'whatiswant'] <- df[,'new_value'] # step 1, use value of new_value
  sapply(1:nrow(df),function(row) { # loop on each row
    x <- df[row,] # take the row, just to use a single var instead later
    ret <- unlist(x['whatiswant']) # initial value
    if(is.na(ret)) { # If empty
      if (x['value'] %in% df$whatiswant) { # test if corresponding value is already present
        ret <- df$value[!df$value %in% df$whatiswant][1] # If yes take the first value not present
      } else {
        ret <- unlist(x['value']) # if not take this value
      }
    }
    if(is.na(ret)) ret <- min(df$value) # No value left, take the min
    df$whatiswant[row] <<- ret # update the df from outside sapply so the next presence test is ok.
  })
  return(df) # return the updated df
}

输出:

>df1[,3] <- NA # Set last column to NA
> res <- t1(df1)
> res
   value new_value whatiswant
1  99.99        NA      99.99
2  99.98        NA      99.96
3  99.97     99.98      99.98
4  99.96        NA      99.95
5  99.95     99.97      99.97
6  99.94        NA      99.94
7  99.93        NA      99.93
8  99.92        NA      99.92
9  99.91        NA      99.91
10 99.90        NA      99.90
11 99.90        NA      99.90
12 99.90        NA      99.90

【讨论】:

  • 编辑:我理解抱歉,正是这一行:这有助于 df$whatiswant[row]
  • 是的,我忘了写关于 &lt;&lt;- 运算符的注释,它在本地范围(函数)和父范围(sapply)中分配
猜你喜欢
  • 1970-01-01
  • 2018-11-30
  • 2022-11-21
  • 2015-04-17
  • 1970-01-01
  • 2018-11-27
  • 2020-05-05
  • 2015-02-07
  • 2021-06-04
相关资源
最近更新 更多