如何在R中按天查找条件差

如何在R中按天查找条件差,r,conditional,posixct,R,Conditional,Posixct,我有一个数字变量,叫做“Blah”。Blah在一天中的不同时间间隔进行测量,并且是一个不断增加的计数。我想找出每天第一次和最后一次观察到的布拉赫之间的差异,并制作一张每天布拉赫增加总量的表格 稍微复杂一点的是,如果Blah足够高,它将重置为一个非常低的数字。这种情况总是以相同的(目前未知的)次数发生,并且以每天一次的最大速率发生 还有一些可能很重要的细节: Blah也在不同的命名位置进行测量。我想要一个按位置列出的一天总数的数据框。:) 时间变量的格式为“mm/dd/yyyy hh:mm:ss”

我有一个数字变量,叫做“Blah”。Blah在一天中的不同时间间隔进行测量,并且是一个不断增加的计数。我想找出每天第一次和最后一次观察到的布拉赫之间的差异,并制作一张每天布拉赫增加总量的表格

稍微复杂一点的是,如果Blah足够高,它将重置为一个非常低的数字。这种情况总是以相同的(目前未知的)次数发生,并且以每天一次的最大速率发生

还有一些可能很重要的细节:

Blah也在不同的命名位置进行测量。我想要一个按位置列出的一天总数的数据框。:)

时间变量的格式为“mm/dd/yyyy hh:mm:ss”

这就是我提出的一个大概的提纲。我遇到的一个问题是,我很少使用POSIXct对象,也不知道如何获取这些值并实现这一点

A<-first value of Day
B<-last value of Day
C<-Maximum value of Blah from a day where reset happens (last value before reset)

For (each Day)
   For (each Location)

     If A < B 
        Then 
           DayTotal = B-A
        Else
            DayTotal = (C-A)+B
(假设上述数据每天都是完整的)我希望结果如下

DESCRIPTION  totalCount     Date
Arch Exit       12       2014-05-23
Northside      2718      2014-05-23
另一次编辑

好的,使用下面的答案,我做了下面的工作,我认为这是可行的

rawDiff是一个已经存在的变量(用excel…yikes完成),parse_date_time是Lubridridate包中的一个函数,“Full”是我的数据,“localdate”是我想要的日期变量

blahblah<-with(Full, tapply(rawDiff, list(parse_date_time(Full$localDate, "mdy"),          DESCRIPTION), function(x) {
sum(x[x>=0])}))
blahblah=0]))

NA有点奇怪,使用一个单独的预制差异变量似乎有帮助。另外,当它重置时,差异为负,所以我只取非负差异。

当像这样寻求帮助时,提供样本数据和所需输出非常有用。因为您没有提供,所以我将使用它(更新以匹配编辑2中的变量名)

日期值的字符串版本为行名,位置为列名或

  droptime(localDateTime) DESCRIPTION rawCount
1              2001-01-01           a      221
2              2001-01-02           a      230
3              2001-01-03           a       32
4              2001-01-01           b      233
5              2001-01-02           b      232
6              2001-01-03           b       34
7              2001-01-01           c      243
8              2001-01-02           c      219
9              2001-01-03           c       36
使用
aggregate
方法(此处保留日期类)


要使用更新的样本数据(编辑1),可以使用

sapply(xx[-1], function(x,g) {
    tapply(x, g, function(x) {
        d <- diff(x)
        d[d<0] <- tail(x,-1)[d<0]
        sum(d)
    })  
}, g=xx[[1]])

@Flick先生的答案可以很容易地根据您的新数据进行调整,但我将分享一个变体来说明,因为您已经定义了逻辑,所以很容易逐字翻译

我们从一个简单的函数开始,该函数查看向量

myFun <- function(x) {
  A <- x[1]                    # What's the first value?
  B <- x[length(x)]            # What's the last value?
  if (B < A) {                 # If the last value is less than the first
    FLAG <- which(diff(x) < 0) # Identify where the value changes...
    C <- x[FLAG]               # ... and extract it
    C - A + B                  # Calculate according to your defined logic
  } else {                     # Otherwise, if things look straightforward
    B - A                      # Just calculate the difference
  }
}
为此,我使用了以下示例数据:

mydf <- structure(list(
  DESCRIPTION = c("Arch Exit", "Arch Exit", "Arch Exit", "Arch Exit", 
                  "Arch Exit", "Arch Exit", "Northside", "Northside", 
                  "Northside", "Northside", "Northside", "Northside", 
                  "Northside", "Northside"), 
  rawCount = c(33166L, 33167L, 33170L, 33173L, 33175L, 33178L, 48073L, 
               48119L, 48167L, 48237L, 73L, 350L, 1430L, 2554L), 
  localDateTime = structure(c(1400831705, 1400832006, 1400832606, 
                              1400832905, 1400833205, 1400833506, 
                              1400943700, 1400943949, 1400944259, 
                              1400944849, 1400945159, 1400945749, 
                              1400946246, 1400947249), 
                            class = c("POSIXct", "POSIXt"), tzone = "GMT")), 
                  .Names = c("DESCRIPTION", "rawCount", "localDateTime"), 
                  row.names = c("1", "2", "3", "4", "5", "6", "7", "8", 
                                "9", "10", "11", "12", "13", "14"), 
                  class = "data.frame")

mydf您真的应该包含一些示例数据和该数据所需的输出。阅读提示和建议。好的,我试着按照你的要求进行编辑。一开始就应该这么做。我的坏朋友。停止改变你的问题,阅读提供的答案哟!我花了几秒钟的时间更改@MrFlick原始答案中的变量名,以反映您的新变量,并得到您期望的答案G.dude bro!我没有改变这个问题,只是改变了数据的样子。:)@迈克尔,这个问题似乎还没有给你答案。你能告诉我你还在哪里吗?太棒了!给我几分钟看看这个。我很感激你的帮助,因为这会让我花上一辈子的时间才明白!哥们,我把数据的格式搞砸了。日期实际上是另一个变量。我已经更新了我的原始答案,以匹配编辑2中的变量名称。感谢您的详细解释。说真的,谢谢您的帮助。很高兴这个网站有这么多人愿意帮忙,因为搜索论坛会让人非常恼火。@Michael,没问题。尽管如此,请尝试提供可复制的数据示例,就像我在回答中所做的和Flick先生在他的回答中所做的一样。它使其他人能够更轻松地复制和粘贴到他们的环境中,并检查答案是否如您所期望的那样工作,从而使每个人的工作更轻松。我希望你不介意我的花花公子和你的模仿:-)哈哈哈……绝对不会。我不容易被淘汰!我喜欢开玩笑。啊,我知道你说的数据是可复制的。我将来会这样做的。
aggregate(rawCount~droptime(localDateTime)+DESCRIPTION, dd, FUN=function(x) {
    d <- diff(x)
    d[d<0] <- tail(x,-1)[d<0]
    sum(d)
})
             a   b   c
2001-01-01 221 233 243
2001-01-02 230 232 219
2001-01-03  32  34  36
  droptime(localDateTime) DESCRIPTION rawCount
1              2001-01-01           a      221
2              2001-01-02           a      230
3              2001-01-03           a       32
4              2001-01-01           b      233
5              2001-01-02           b      232
6              2001-01-03           b       34
7              2001-01-01           c      243
8              2001-01-02           c      219
9              2001-01-03           c       36
sapply(xx[-1], function(x,g) {
    tapply(x, g, function(x) {
        d <- diff(x)
        d[d<0] <- tail(x,-1)[d<0]
        sum(d)
    })  
}, g=xx[[1]])
  06/24/2014 06/25/2014
A          8         52
B          4         57
myFun <- function(x) {
  A <- x[1]                    # What's the first value?
  B <- x[length(x)]            # What's the last value?
  if (B < A) {                 # If the last value is less than the first
    FLAG <- which(diff(x) < 0) # Identify where the value changes...
    C <- x[FLAG]               # ... and extract it
    C - A + B                  # Calculate according to your defined logic
  } else {                     # Otherwise, if things look straightforward
    B - A                      # Just calculate the difference
  }
}
aggregate(rawCount ~ DESCRIPTION + as.Date(localDateTime), mydf, myFun)
#   DESCRIPTION as.Date(localDateTime) rawCount
# 1   Arch Exit             2014-05-23       12
# 2   Northside             2014-05-24     2718
mydf <- structure(list(
  DESCRIPTION = c("Arch Exit", "Arch Exit", "Arch Exit", "Arch Exit", 
                  "Arch Exit", "Arch Exit", "Northside", "Northside", 
                  "Northside", "Northside", "Northside", "Northside", 
                  "Northside", "Northside"), 
  rawCount = c(33166L, 33167L, 33170L, 33173L, 33175L, 33178L, 48073L, 
               48119L, 48167L, 48237L, 73L, 350L, 1430L, 2554L), 
  localDateTime = structure(c(1400831705, 1400832006, 1400832606, 
                              1400832905, 1400833205, 1400833506, 
                              1400943700, 1400943949, 1400944259, 
                              1400944849, 1400945159, 1400945749, 
                              1400946246, 1400947249), 
                            class = c("POSIXct", "POSIXt"), tzone = "GMT")), 
                  .Names = c("DESCRIPTION", "rawCount", "localDateTime"), 
                  row.names = c("1", "2", "3", "4", "5", "6", "7", "8", 
                                "9", "10", "11", "12", "13", "14"), 
                  class = "data.frame")