-1

I have the following data: Close dn mavg up pctB sig

2014-11-21 13:40:00 120462.8 120244.0 120427.2 120610.4 0.5973438 1 2014-11-21 13:45:00 120461.1 120282.7 120414.9 120547.0 0.6747622 0 2014-11-21 13:50:00 120635.2 120267.1 120418.6 120570.2 1.2145544 0 2014-11-21 13:55:00 120545.0 120266.0 120419.2 120572.4 0.9105750 -1 2014-11-21 14:00:00 120495.9 120265.5 120422.2 120578.9 0.7351776 -1 2014-11-21 14:05:00 120507.5 120267.1 120427.6 120588.2 0.7487046 -1

I am trying to reset the sig column to 0 every day when time is 14:00 (the result in the example above, will be that the value of sig in the 5th line will be 0 and not -1). I checked around but could not find how to make a condition based on part of the first column only (which is "data time" and my condition is time only).

Any advice will be helpfull.

Thank you!

Arik Noi
  • 3
  • 2
  • 1
    Your example is not reproducible and your question is almost unreadable. Please consider improving your question http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – ECII Nov 23 '14 at 21:44

1 Answers1

0

I tried interpreting your question. This is what your data looks like:

                         close       dn     mavg       up      pctb sig
1 11/21/2014 13:40:00 120462.8 120244.0 120427.2 120610.4 0.5973438   1
2 11/21/2014 13:45:00 120461.1 120282.7 120414.9 120547.0 0.6747622   0
3 11/21/2014 13:50:00 120635.2 120267.1 120418.6 120570.2 1.2145544   0
4 11/21/2014 13:55:00 120545.0 120266.0 120419.2 120572.4 0.9105750  -1
5 11/21/2014 14:00:00 120495.9 120265.5 120422.2 120578.9 0.7351776  -1
6 11/21/2014 14:05:00 120507.5 120267.1 120427.6 120588.2 0.7487046  -1

Try this:

dateTimes <- as.character(index(spread_BB))
time <- ldply(strsplit(dateTimes, ' '))[, 2]
df <- data.frame(time = time, sig = spread_BB$sig)
sig <- with(df, ifelse(time == '14:00:00', 0, sig))
spread_BB$sig <- NULL
spread_BB$sig <- sig

#> spread_BB
#                          close       dn     mavg       up      pctb sig
# 1 11/21/2014 13:40:00 120462.8 120244.0 120427.2 120610.4 0.5973438   1
# 2 11/21/2014 13:45:00 120461.1 120282.7 120414.9 120547.0 0.6747622   0
# 3 11/21/2014 13:50:00 120635.2 120267.1 120418.6 120570.2 1.2145544   0
# 4 11/21/2014 13:55:00 120545.0 120266.0 120419.2 120572.4 0.9105750  -1
# 5 11/21/2014 14:00:00 120495.9 120265.5 120422.2 120578.9 0.7351776   0
# 6 11/21/2014 14:05:00 120507.5 120267.1 120427.6 120588.2 0.7487046  -1
maloneypatr
  • 3,562
  • 4
  • 23
  • 33
  • thanks for the detailed answer @maloneypatr. I am using xts format for the dataset and I get an error when running thr first line datetimes <- ldply(strsplit(dat2$datetime,' ')) ->Error in strsplit(spread_BB$row.names, " ") : non-character argument. In my dataset, spread_BB, the first column is not Close and not data/time which is named row.names. I am sure doing something wrong. thanks again. – Arik Noi Nov 24 '14 at 06:45
  • Gotcha...Try the updated answer. I didn't know you were using an `xts` object. In the future, type `dput(spread_BB)` and insert that into your question. It will be much easier to understand the data you're working with and replicate the issue you are facing. – maloneypatr Nov 24 '14 at 18:19
  • @ArikNoi - you should state in your post that the data is xts. It is important – Rich Scriven Nov 24 '14 at 18:21
  • @maloneypatr I am sorry about the missing XTD info, will do next time. It worked. thanks. 1 follow up q. how do i change from ==14:00:00 to >= 14:00:00? – Arik Noi Nov 24 '14 at 19:50