logo
Tags down

shadow

using dplyr to calculate consecutive days with a particular value


By : Calemb
Date : October 17 2020, 06:10 PM
I wish this helpful for you You could create a grouping variable using cumsum and then use row_number to generate the consecutive days when the temperature was less than 0.
code :
library(dplyr)

dat %>%
  group_by(group = cumsum(temp > 0)) %>%
  mutate(tempdays = row_number() - 1) %>%
  ungroup() %>%
  select(-group)


#    date      temp tempdays
#   <date>     <dbl>    <dbl>
# 1 2010-01-01  4.2         0
# 2 2010-01-02  3.35        0
# 3 2010-01-03 -0.6         1
# 4 2010-01-04 -0.25        2
# 5 2010-01-05  0.8         0
# 6 2010-01-06  0.8         0
# 7 2010-01-07 -2.5         1
# 8 2010-01-08 -5.25        2
# 9 2010-01-09 -0.5         3
#10 2010-01-10  3.35        0
with(dat, ave(temp, cumsum(temp > 0), FUN = seq_along) - 1)
dat2 %>%
  mutate(tempdays = with(rle(temp < 0), rep(values, lengths))) %>%
  group_by(group = cumsum(temp > 0)) %>%
  mutate(tempdays = cumsum(tempdays)) %>%
  ungroup() %>%
  select(-group)


#      date    temp   tempdays
#     <date>   <dbl>    <int>
# 1 2010-01-01 -1.95        1
# 2 2010-01-02 -1.1         2
# 3 2010-01-03 -2.8         3
# 4 2010-01-04 -6.7         4
# 5 2010-01-05  1.4         0
# 6 2010-01-06  4.45        0
# 7 2010-01-07  6.1         0
# 8 2010-01-08  4.7         0
# 9 2010-01-09 -1.7         1
#10 2010-01-10 -3.9         2


Share : facebook icon twitter icon

calculate difference of value for consecutive days for all objects in sql


By : user2795021
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I have a table with an object, index, date and value:
code :
SELECT t2.object,t2.index,t2.date,t2.value-t1.value
FROM table t1, table t2
    WHERE t1.object=t2.object AND t1.index=t2.index
        AND t2.date=DATE_ADD(t1.date, INTERVAL 1 DAY);

Consecutive Days by Group - Error in Mutate Function Dplyr


By : ginerjm
Date : March 29 2020, 07:55 AM
this one helps. The issue is that for some groups, the row for yesterday is not found. This can be fixed by defining a function that checks for that instead of inlining the function in mutate:
code :
library(dplyr)
compute.consec.days <- function(date, callout, yesterday, rown) {
  j <- which(date == yesterday)
  if (length(j)==0) NA else cumsum(rev(cumprod(rev((yesterday-date)==(j-rown) & callout==callout[date == yesterday]))))
}
yesterday <- as.Date("2016-10-30")
out <- employ.data %>% group_by(Child) %>%
  mutate(`Consec. Days with Callout`=compute.consec.days(DATE,Callout,yesterday,row_number())) %>%
  filter(DATE == yesterday)
##Source: local data frame [2 x 7]
##Groups: Child [2]
##
##        DATE Parent  Child avg_child_salary  salary Callout Consec. Days with Callout
##      <date> <fctr> <fctr>            <dbl>   <dbl>  <fctr>                     <dbl>
##1 2016-10-30      A     ac           526.27    0.00     LOW                         1
##2 2016-10-30      G     gb         18089.83 6973.54     LOW                         1
compute.consec.days <- function(date, callout, yesterday, rown) {
  j <- which(date == yesterday)
  if (length(j)==0) NA else {
    ## first compute the condition
    cond <- (yesterday-date)==(j-rown) & callout==callout[date == yesterday]
    ## then evaluate consecutive days only with this vector up to
    ## the row corresponding to yesterday. Then add the result with NAs
    ## because mutate is a windowing function
    c(cumsum(rev(cumprod(rev(cond[1:j[1]])))),rep(NA,length(date)-j[1]))
  }
}
yesterday <- as.Date("2016-10-20")
out <- employ.data %>% group_by(Child) %>%
  mutate(`Consec. Days with Callout`=compute.consec.days(DATE,Callout,yesterday,row_number())) %>%
  filter(DATE == yesterday)
##Source: local data frame [4 x 7]
##Groups: Child [4]
##
##        DATE Parent  Child avg_child_salary   salary Callout Consec. Days with Callout
##      <date> <fctr> <fctr>            <dbl>    <dbl>  <fctr>                     <dbl>
##1 2016-10-20      A     ae          1197.25  2238.56    HIGH                         1
##2 2016-10-20      B     ba            21.68    43.22    HIGH                         1
##3 2016-10-20      G     ga           582.66  1947.35    HIGH                         2
##4 2016-10-20      G     gb         18089.83 31158.51    HIGH                         2
yesterday <- as.Date("2016-10-30")
out <- employ.data %>% group_by(Child) %>%
  mutate(`Consec. Days with Callout`=compute.consec.days(DATE,Callout,yesterday,row_number())) %>%
  filter(DATE == yesterday)
##Source: local data frame [2 x 7]
##Groups: Child [2]
##
##        DATE Parent  Child avg_child_salary  salary Callout Consec. Days with Callout
##      <date> <fctr> <fctr>            <dbl>   <dbl>  <fctr>                     <dbl>
##1 2016-10-30      A     ac           526.27    0.00     LOW                         1
##2 2016-10-30      G     gb         18089.83 6973.54     LOW                         1

Climate: Calculate Consecutive Days of Rain


By : Tom Morrison
Date : March 29 2020, 07:55 AM
this one helps. Following from Ted D's comment... a possible formula for the second helper column would be (in cell G2):
code :
=IF(F2=0,0,MATCH(0,F3:F999,0)-1+F2)
=IF($G2=VALUE(SUBSTITUTE(MID(H$1,3,999),"day","")),$F2,"")

How to calculate the difference between consecutive sets of two columns in a column range using dplyr


By : Avinash Tompe
Date : March 29 2020, 07:55 AM
I hope this helps you . Here's a less advanced approach using dplyr and tidyr verbs. First I gather the columns for differencing into long format, then take their differences vs. previous column, strip out NA's for the first columns which have no previous column, rename the column, spread out, and attach onto the original.
code :
library(tidyverse)
iris %>%
  bind_cols(iris %>%
    rowid_to_column() %>%
    gather(col, val, Sepal.Width:Petal.Width) %>%
    group_by(rowid) %>%
    mutate(val = abs(val - lag(val))) %>%
    filter(!is.na(val)) %>%
    mutate(col = paste0("diff_", col)) %>%
    spread(col, val)  %>%
    select(contains("diff"))
  )


    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species rowid diff_Petal.Length diff_Petal.Width
1            5.1         3.5          1.4         0.2     setosa     1               2.1              1.2
2            4.9         3.0          1.4         0.2     setosa     2               1.6              1.2
3            4.7         3.2          1.3         0.2     setosa     3               1.9              1.1
4            4.6         3.1          1.5         0.2     setosa     4               1.6              1.3
5            5.0         3.6          1.4         0.2     setosa     5               2.2              1.2
6            5.4         3.9          1.7         0.4     setosa     6               2.2              1.3
7            4.6         3.4          1.4         0.3     setosa     7               2.0              1.1

dplyr: calculate the days for product replenishment


By : user3345062
Date : March 29 2020, 07:55 AM
around this issue I hope someone else finds a cleaner solution. But this produces diffDate which assigns the date difference from when a negative turns positive/zero.
code :
sample_df %>%
  mutate(sign = ifelse(Net_Available_Qty > 0, "pos", ifelse(Net_Available_Qty < 0, "neg", "zero")),
         sign_lag = lag(sign, default = sign[1]),       # get previous value (exception in the first place)
         change = ifelse(sign != sign_lag, 1 , 0),      # check if there's a change
         sequence=sequence(rle(as.character(sign))$lengths)) %>%
  group_by(sequence) %>%
  mutate(diffDate = as.numeric(difftime(Date, lag(Date,1))),
         diffDate=ifelse(Net_Available_Qty <0, NA, ifelse((sign=='pos'| sign=='zero') & sequence==1, diffDate, NA))) %>% 
  ungroup() %>%
  select(Date, Product, Net_Available_Qty, diffDate)
Related Posts Related Posts :
  • Normalizing a dataframe having several conditions, R
  • What is the equivalent of "everything()" operator in "data.table"?
  • The colors in ggplot do not correspond to the colors in the legend
  • How to add to each column of dataframe value from named vector?
  • Losing data while saving csv character file
  • How to target arbitrary HTML attribute containing space in R?
  • Iterate over a column and find the sum
  • reference another column with tidyeval in filter
  • How to create a new variable by comparing two other variables
  • Retrieving elements that are occurring after adding a fixed number from a start element in a vector
  • Get total mean for variables nested in a list of lists of lists
  • Render Xaringan Rmd when using _site.yml file
  • Regex: matching multiple patterns and getting middle of string
  • removing for loop with sample function
  • R: scraping additional data after POST only works for first page
  • R - Add ticks on top of plot with levels in variable?
  • How to avoid factors in R when reading csv data
  • Meaning of :: syntax
  • How to get month and week of the month from year and week no in R?
  • Can you save R code files with .RData objects for version control?
  • How to connect points of two different plots to each other using ggplot in R?
  • Is there an R function for finding the value below specific value
  • R group_by return number of largest unique type
  • Different ways of selecting columns inside function resulting in different results, why?
  • How do I join a Y variable to each X variable in a dataframe?
  • World map: filtering by 'subregion' removes many regions
  • Generate data frame with parameters
  • Star (*) notation in R session Information
  • Why does formals function return NULL on functions defined with arguments?
  • How to call a list style parameter in snakemake
  • as_tibble only returns a single variable
  • Can't create design matrix from user input
  • R - how to sum each columns from df
  • R devtools::check LICENSE is not mentioned and other issues in DESCRIPTION FILE
  • Simple arithmetic leads to floating point difference in R
  • why does the data I input into R plot function change?
  • How can I import my data.frame as an igraph object?
  • Join each row with each other row
  • Join complementary dataframes, no NAs where a value is available
  • How to restart R and continue a benchmark script from previous line (on Windows)?
  • When and how to use as.name() vs.get() in data.table (ex. in looping over columns)?
  • How to combine similar strings showing most common characters
  • Adjust spacing between text and chunk output in a R Markdown PDF document
  • Transform data to use lubridate on it
  • I need to know why I get the error 'unexpected input in "p<-ggplot(data=mov2, aes(x=Genre,y=Gross % US))" '
  • ggplot different lm formulas
  • change border color of a county in ggplot in R
  • position_dodgev causes error in order of connecting points in geom_line
  • How can I delete lines in which the name appears only once?
  • mutate_if, summarize_at etc coerce data.table to data.frame
  • How to get different values for same ID in dataframe. And replace any of that different value for the same ID
  • Lagging data based on condition (non-fix lag)
  • How to use 'sparklyr::replace.na()' for replacing NaN on one column?
  • How to create lollipop graph
  • R: Why is pmap not working while map2 does?
  • How to have different legends and colour schemes for different geom_*(aes(col= ) in ggplot?
  • How to check if a value under condition is within an interval under other condition in R?
  • Remove character string from multiple columns in R
  • subset a data frame with dplyr and conditions
  • R: How to show forecast and actual data in a single plot?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk