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

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

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

问题

我尝试将特定时间点标记为零点,然后将前面的行设置为降序整数(-1、-2等),后面的行设置为升序整数(+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")
num = c(1000, 1200, 1234, 980, 1300)
df <- data.frame(Time = rep(Time, num), num = sequence(num))    
df[paste0('rand', seq_along(Time))] <- rnorm(length(Time) * sum(num))

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:

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;)
num = c(1000,1200,1234,980,1300)
df &lt;- data.frame(Time = rep(Time, num), num = sequence(num))    
df[paste0(&#39;rand&#39;, seq_along(Time))] &lt;- rnorm(length(Time) * sum(num))

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

以下是翻译好的部分:

如果您的目标是根据给定的“零点”创建一个升序或降序的新列,您可以尝试以下方法。

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")
num = c(1000, 1200, 1234, 980, 1300)
df <- data.frame(Time = rep(Time, num), num = sequence(num))    
df[paste0('rand', seq_along(Time))] <- rnorm(length(Time) * sum(num))

head(df)

zero = "2023-07-28 22:16:00"
id_col = 'Time'

add_index = function(df, zero, id_col){

  df = df[order(df[, id_col]),]
  df$new_col = c(-rev(sequence(sum(df[, id_col] < zero))), rep(0, sum(df[, id_col] == zero)), sequence(sum(df[, id_col] > zero)))

  return(df)
}

df %>% 
  add_index(zero, id_col) %>% 
  head()

还有以下输出结果:

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

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;)
num = c(1000,1200,1234,980,1300)
df &lt;- data.frame(Time = rep(Time, num), num = sequence(num))    
df[paste0(&#39;rand&#39;, seq_along(Time))] &lt;- rnorm(length(Time) * sum(num))

head(df)

zero = &quot;2023-07-28 22:16:00&quot;
id_col = &#39;Time&#39;

add_index = function(df,zero,id_col){
  
  df = df[order(df[,id_col]),]
  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)))
  
  return(df)
}

df %&gt;% 
  add_index(zero,id_col) %&gt;% 
  head()

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

                 Time num      rand1       rand2       rand3       rand4      rand5 new_col
1 2023-07-28 22:14:00   1  0.2995052  1.64549561 -0.79780340 -1.07715548  0.4070298   -2200
2 2023-07-28 22:14:00   2 -1.6490923 -0.35256450  0.06296558  0.06870601 -0.3095747   -2199
3 2023-07-28 22:14:00   3 -0.5708513 -1.65916717  0.83986290 -2.09933862  0.3588580   -2198
4 2023-07-28 22:14:00   4 -0.3760261  0.89072105  0.56148014  0.08404197 -0.6248979   -2197
5 2023-07-28 22:14:00   5 -1.2998215  0.62532664  0.14171299 -0.25804508  0.5117224   -2196
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:

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

Please let me know if you need any further assistance.

英文:
df %&gt;%
 	mutate(Time = ymd_hms(Time), 
 		   dif = Time - ymd_hms(&#39;2023-07-28 22:16:00&#39;),
 		   rank = dense_rank(dif) - row_number()[dif== 0])

                 Time  num       dif rank
1 2023-07-28 22:14:00 1000 -120 secs   -2
2 2023-07-28 22:15:00 1200  -60 secs   -1
3 2023-07-28 22:16:00 1234    0 secs    0
4 2023-07-28 22:17:00  980   60 secs    1
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:

确定