将时间点设置为零,然后上升和下降的整数围绕着。

huangapple go评论122阅读模式
英文:

R: Set Time Point as Zero and then Ascending and Descending Integers Around

问题

我尝试将特定时间点标记为零点,然后将前面的行设置为降序整数(-1、-2等),后面的行设置为升序整数(+1、+2等),但我完全不知道如何做到这一点。这是一个示例数据集:

  1. Time = c("2023-07-28 22:14:00", "2023-07-28 22:15:00", "2023-07-28 22:16:00", "2023-07-28 22:17:00", "2023-07-28 22:18:00")
  2. num = c(1000, 1200, 1234, 980, 1300)
  3. df <- data.frame(Time = rep(Time, num), num = sequence(num))
  4. df[paste0('rand', seq_along(Time))] <- rnorm(length(Time) * sum(num))
  5. head(df)

目标是将2023-07-28 22:16:00设为0,然后在时间点之前设置降序整数,在时间点之后设置升序整数。谢谢您提前的帮助。

英文:

I'm trying to make mark a specific time point as point zero and then the previous rows set as descending integers (-1, -2, etc) and the following rows as ascending integers (+1, +2, etc), but I'm completely stumped on how to do this. Here is a sample dataset:

  1. Time = c(&quot;2023-07-28 22:14:00&quot;, &quot;2023-07-28 22:15:00&quot;, &quot;2023-07-28 22:16:00&quot;, &quot;2023-07-28 22:17:00&quot;, &quot;2023-07-28 22:18:00&quot;)
  2. num = c(1000,1200,1234,980,1300)
  3. df &lt;- data.frame(Time = rep(Time, num), num = sequence(num))
  4. df[paste0(&#39;rand&#39;, seq_along(Time))] &lt;- rnorm(length(Time) * sum(num))
  5. head(df)

The goal would be to make 2023-07-28 22:16:00 = 0 and then descending integers before and ascending integers after time point. Thanks in advance

答案1

得分: 1

以下是翻译好的部分:

  1. 如果您的目标是根据给定的“零点”创建一个升序或降序的新列,您可以尝试以下方法。
  2. Time = c("2023-07-28 22:14:00", "2023-07-28 22:15:00", "2023-07-28 22:16:00", "2023-07-28 22:17:00", "2023-07-28 22:18:00")
  3. num = c(1000, 1200, 1234, 980, 1300)
  4. df <- data.frame(Time = rep(Time, num), num = sequence(num))
  5. df[paste0('rand', seq_along(Time))] <- rnorm(length(Time) * sum(num))
  6. head(df)
  7. zero = "2023-07-28 22:16:00"
  8. id_col = 'Time'
  9. add_index = function(df, zero, id_col){
  10. df = df[order(df[, id_col]),]
  11. df$new_col = c(-rev(sequence(sum(df[, id_col] < zero))), rep(0, sum(df[, id_col] == zero)), sequence(sum(df[, id_col] > zero)))
  12. return(df)
  13. }
  14. df %>%
  15. add_index(zero, id_col) %>%
  16. head()

还有以下输出结果:

  1. Time num rand1 rand2 rand3 rand4 rand5 new_col
  2. 1 2023-07-28 22:14:00 1 0.2995052 1.64549561 -0.79780340 -1.07715548 0.4070298 -2200
  3. 2 2023-07-28 22:14:00 2 -1.6490923 -0.35256450 0.06296558 0.06870601 -0.3095747 -2199
  4. 3 2023-07-28 22:14:00 3 -0.5708513 -1.65916717 0.83986290 -2.09933862 0.3588580 -2198
  5. 4 2023-07-28 22:14:00 4 -0.3760261 0.89072105 0.56148014 0.08404197 -0.6248979 -2197
  6. 5 2023-07-28 22:14:00 5 -1.2998215 0.62532664 0.14171299 -0.25804508 0.5117224 -2196
  7. 6 2023-07-28 22:14:00 6 -0.4678034 0.03267064 0.76075794 -0.32648332 -3.3211990 -2195

希望这有助于您理解代码和输出。

英文:

EDIT: using the updated code in the question...

If your goal is to create a new column with numbers ascending or descending depending on a given "zero-point" you could try something like this.

  1. Time = c(&quot;2023-07-28 22:14:00&quot;, &quot;2023-07-28 22:15:00&quot;, &quot;2023-07-28 22:16:00&quot;, &quot;2023-07-28 22:17:00&quot;, &quot;2023-07-28 22:18:00&quot;)
  2. num = c(1000,1200,1234,980,1300)
  3. df &lt;- data.frame(Time = rep(Time, num), num = sequence(num))
  4. df[paste0(&#39;rand&#39;, seq_along(Time))] &lt;- rnorm(length(Time) * sum(num))
  5. head(df)
  6. zero = &quot;2023-07-28 22:16:00&quot;
  7. id_col = &#39;Time&#39;
  8. add_index = function(df,zero,id_col){
  9. df = df[order(df[,id_col]),]
  10. df$new_col = c(-rev(sequence(sum(df[,id_col] &lt; zero))),rep(0,sum(df[,id_col] == zero)),sequence(sum(df[,id_col] &gt; zero)))
  11. return(df)
  12. }
  13. df %&gt;%
  14. add_index(zero,id_col) %&gt;%
  15. head()

There are probably other ways of doing this, but here is the output of this approach:

  1. Time num rand1 rand2 rand3 rand4 rand5 new_col
  2. 1 2023-07-28 22:14:00 1 0.2995052 1.64549561 -0.79780340 -1.07715548 0.4070298 -2200
  3. 2 2023-07-28 22:14:00 2 -1.6490923 -0.35256450 0.06296558 0.06870601 -0.3095747 -2199
  4. 3 2023-07-28 22:14:00 3 -0.5708513 -1.65916717 0.83986290 -2.09933862 0.3588580 -2198
  5. 4 2023-07-28 22:14:00 4 -0.3760261 0.89072105 0.56148014 0.08404197 -0.6248979 -2197
  6. 5 2023-07-28 22:14:00 5 -1.2998215 0.62532664 0.14171299 -0.25804508 0.5117224 -2196
  7. 6 2023-07-28 22:14:00 6 -0.4678034 0.03267064 0.76075794 -0.32648332 -3.3211990 -2195

答案2

得分: 1

Sure, here's the translated code with the requested part:

  1. df %>%
  2. mutate(Time = ymd_hms(Time),
  3. dif = Time - ymd_hms('2023-07-28 22:16:00'),
  4. rank = dense_rank(dif) - row_number()[dif == 0]) %>%
  5. select(-dif)

Please let me know if you need any further assistance.

英文:
  1. df %&gt;%
  2. mutate(Time = ymd_hms(Time),
  3. dif = Time - ymd_hms(&#39;2023-07-28 22:16:00&#39;),
  4. rank = dense_rank(dif) - row_number()[dif== 0])
  5. Time num dif rank
  6. 1 2023-07-28 22:14:00 1000 -120 secs -2
  7. 2 2023-07-28 22:15:00 1200 -60 secs -1
  8. 3 2023-07-28 22:16:00 1234 0 secs 0
  9. 4 2023-07-28 22:17:00 980 60 secs 1
  10. 5 2023-07-28 22:18:00 1300 120 secs 2

You can then remove the dif column

huangapple
  • 本文由 发表于 2023年8月4日 21:53:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76836593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定