fork download
  1.  
  2. library(dplyr)
  3.  
  4. set.seed(1234)
  5. df <- data.frame(Date = seq(Sys.Date()- 19 , Sys.Date(), 1) ,
  6. PnL = runif(20, -150, 100))
  7.  
  8. df %>% mutate(sign = ifelse(PnL > 0 , "pos", "neg"),
  9. times = sequence(rle(as.character(sign))$lengths) - 1,
  10. GrZTimes = ifelse(sign == "pos" ,times , 0),
  11. LeZTimes = ifelse(sign == "neg" ,times , 0))
  12.  
Success #stdin #stdout #stderr 0.54s 205440KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

Error in mutate_impl(.data, dots) : invalid subscript type 'closure'
Calls: %>% ... as.data.frame -> mutate_ -> mutate_.tbl_df -> mutate_impl -> .Call
Execution halted