【问题标题】:How to add values in one column that have '0' in another column如何在一列中添加另一列中具有“0”的值
【发布时间】:2019-06-06 01:01:10
【问题描述】:

我有一个包含 Time.Interval、Net.Chg 和 Tick.Count 列的数据集。 Net.Chg 有正数、负数和零。基于 Net.Chg,我想对 Time.Count 中的正值、负值和零值求和,然后按日期分组。

Time.Interval   Net.Chg    Tick.Count
2-Jan-17         NA        NA
19:15 - 19:16       -0.0047    7
19:16 - 19:17    0     8
19:17 - 19:18    0.0025    10
3-Jan-17         NA        NA
03:45 - 03:46    0     1
03:54 - 03:55   -0.0002    2
19:43 - 19:44   -0.0008    4
20:01 - 20:02    0.0025    2
4-Jan-17         NA        NA
00:54 - 00:55   -0.0007    2
01:10 - 01:11    0.0005    1
01:11 - 01:12    0     1
Time.Interval <- c('2-Jan-17 _00:00:00.000000', '19:15 - 19:16', '19:16 - 19:17', '19:17 - 19:18', '3-Jan-17 _00:00:00.000000', '03:45 - 03:46', '03:54 - 03:55', '19:43 - 19:44', '20:01 - 20:02', '4-Jan-17 _00:00:00.000000', '00:54 - 00:55', '01:10 - 01:11', '01:11 - 01:12')
Net.Chg <- c(NA, -0.0047, 0, 0.0025, NA, 0, -0.0002, -0.0008, 0.0025, NA, -0.0007, 0.0005, 0)
Tick.Count <-  c(NA, 7, 8, 10, NA, 1, 2, 4, 2, NA, 2, 1, 1)
data <- data.frame(Time.Interval, Net.Chg, Tick.Count)

需要的输出是

pos = sum of "Tick.Count" if Net.Chg > 0
neg = sum of "Tick.Count" if Net.Chg < 0
UnChng = sum of "Tick.Count" if Net.Chg == 0
OF <- pos - Neg

我尝试了以下代码

DF <- dd %>% group_by(grp = cumsum(str_detect(Time.Interval, "[A-Z]"))) %>% summarise(Time.Interval = anydate(first(Time.Interval)), pos = sum((Net.Chg > 0)* Tick.Count, na.rm = T),  neg = sum((Net.Chg < 0) * Tick.Count, na.rm = T), unChg = sum(Net.Chg ==0 * Tick.Count, na.rm=T), OF = sum(sign(Net.Chg) * Tick.Count, na.rm = TRUE))

此代码为我提供了正确的 posneg 和 'OF' 值,但 Unchng 的值是错误的。

当前输出为

Time.Interval      pos    Neg     UnChng     OF
02Jan2017          10     7       4           3      
03Jan2017          2      6       5          -4
04Jan2017          1      2       4          -1

而实际输出应该是

Time.Interval      pos    Neg     UnChng     OF
02Jan2017          10     7       8           3      
03Jan2017          2      6       1          -4
04Jan2017          1      2       1          -1

我尝试了sum(Net.Chg ==0 + Tick.Count, na.rm=T)length(Net.Chg ==0 * Tick.Count),但没有成功。

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    由于精度错误,在比较浮点数时,切勿使用==。 R 具有 all.equalidentical 之类的功能,或者您可以只检查小错误,例如。

    DF <- dd %>% 
        group_by(grp = cumsum(str_detect(Time.Interval, "[A-Z]"))) %>% 
        summarise(Time.Interval = anydate(first(Time.Interval)), 
            pos = sum((Net.Chg > 0)* Tick.Count, na.rm = TRUE),  
            neg = sum((Net.Chg < 0) * Tick.Count, na.rm = TRUE), 
            unChg = sum((abs(Net.Chg)-0 < 1e-15) * Tick.Count, na.rm=TRUE), 
            OF = sum(sign(Net.Chg) * Tick.Count, na.rm = TRUE))
    

    使用T 而不是TRUE 也被认为是不好的做法,因为前者可以设置为任何值。

    【讨论】:

    • 非常感谢 @enesaisquoi 的回答和最佳实践指导
    【解决方案2】:

    你需要得到对应的Tick.Count where Net.Chg ==0 and sum 就可以了。

    library(anytime)
    library(tidyverse)
    
    data %>% 
      group_by(grp = cumsum(str_detect(Time.Interval, "[A-Z]"))) %>% 
      summarise(Time.Interval = anydate(first(Time.Interval)), 
                pos = sum((Net.Chg > 0)* Tick.Count, na.rm = TRUE),  
                neg = sum((Net.Chg < 0) * Tick.Count, na.rm = TRUE), 
                unChg = sum(Tick.Count[Net.Chg ==0], na.rm = TRUE), 
                OF = sum(sign(Net.Chg) * Tick.Count, na.rm = TRUE)) %>%
      ungroup() %>%
      select(-grp)
    
    #  Time.Interval   pos   neg unChg    OF
    #  <date>        <dbl> <dbl> <dbl> <dbl>
    #1 02Jan2017       10     7     8     3
    #2 03Jan2017        2     6     1    -4
    #3 04Jan2017        1     2     1    -1
    

    【讨论】:

      猜你喜欢
      • 2014-05-19
      • 2015-07-04
      • 1970-01-01
      • 2016-07-06
      • 2021-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多