Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

r - count number of occurences in different timeline

I have this kind of data.

library(dplyr)
library(tidyverse)

df <- tibble(mydate = as.Date(c("2019-05-11 23:01:00", "2019-05-11 23:02:00", "2019-05-11 23:03:00", "2019-05-11 23:04:00",
                                "2019-05-12 23:05:00", "2019-05-12 23:06:00", "2019-05-12 23:07:00", "2019-05-12 23:08:00",
                                "2019-05-13 23:09:00", "2019-05-13 23:10:00", "2019-05-13 23:11:00", "2019-05-13 23:12:00",
                                "2019-05-14 23:13:00", "2019-05-14 23:14:00", "2019-05-14 23:15:00", "2019-05-14 23:16:00",
                                "2019-05-15 23:17:00", "2019-05-15 23:18:00", "2019-05-15 23:19:00", "2019-05-15 23:20:00")),
               myval = c(0, NA, 1500, 1500,
                         1500, 1500, NA, 0,
                         0, 0, 1100, 1100,
                         1100, 0, 200, 200,
                         1100, 1100, 1100, 0
               ))

I want to divide every same value with the counts that it appears. But, if between this number (value 1100) , another number (or NA) appears, and then re-appears (value 1100) , I want to count it separatable.

# just replace values [0,1] with NA
df$myval[df$myval >= 0 & df$myval <= 1] <- NA

df <- df %>%
    group_by(myval) %>%
    mutate(counts = sum(myval == myval)) %>%
    mutate(result = (myval  / counts))

Right now the result is:

 mydate     myval counts result
   <date>     <dbl>  <int>  <dbl>
 1 2019-05-11    NA     NA    NA 
 2 2019-05-11    NA     NA    NA 
 3 2019-05-11  1500      4   375 
 4 2019-05-11  1500      4   375 
 5 2019-05-12  1500      4   375 
 6 2019-05-12  1500      4   375 
 7 2019-05-12    NA     NA    NA 
 8 2019-05-12    NA     NA    NA 
 9 2019-05-13    NA     NA    NA 
10 2019-05-13    NA     NA    NA 
11 2019-05-13  1100      6   183.
12 2019-05-13  1100      6   183.
13 2019-05-14  1100      6   183.
14 2019-05-14    NA     NA    NA 
15 2019-05-14   200      2   100 
16 2019-05-14   200      2   100 
17 2019-05-15  1100      6   183.
18 2019-05-15  1100      6   183.
19 2019-05-15  1100      6   183.
20 2019-05-15    NA     NA    NA 

but as you cane see for the value 1100 that appears twice, it count it 6 times. I want to count it 3 times and then again 3 times.

So, for example value 1500 appears 4 times, so I divide 1500/4. 1100 should be divided by 3 and then again by 3.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do that using Run Length Encoding (which is basically a cumulative sum that restarts when it sees another value).

rle(df$myval) %$%
  tibble(rle = lengths,
         myval = values,
         avg = values / rle)
# A tibble: 10 x 3
#     rle myval   avg
#    <int> <dbl> <dbl>
# 1     1     0    0 
# 2     1    NA   NA 
# 3     4  1500  375 
# 4     1    NA   NA 
# 5     3     0    0 
# 6     3  1100  367.
# 7     1     0    0 
# 8     2   200  100 
# 9     3  1100  367.
# 10     1     0    0 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...