0

I have a question about a problem with my data analysis. I have a large data set with sentiment scores for every day of the week, but I want to add the weekends to the Fridays. This for the reason that I can make stock predictions for the Monday. My idea was to give them the same date and afterwards I aggregate them on daily basis. An example of the relevant part of my data output is:

year: 2019-01-01 2019-01-02 2019-01-03 2019-01-04 2019-01-05 2019-01-06 2019-01-07 UTC" weekday: 1 2 3 4 5 6 7

so the aim is to get the dates of 7(Saturday) & 1(Sunday) equal to that of 6(Friday)

I have tried a couple of codes to realize this:

1)

ifelse(Brexit_data$weekday == '7', as.POSIXct(Brexit_data$year - as.difftime(1, units = "days")), as.POSIXct(Brexit_data$year - as.difftime(0, units = "days")))`

ifelse(Brexit_data$weekday == '7', as.date(Brexit_data$year) -1, as.date(Brexit_data$year))

Both returned a different structure type of the dates like:

[1] 1549065600 1549065600 1549065600 1549065600 1549065600 1549065600 1549065600 1549065600 1549065600 1549065600 1549065600 1549065600 1549065600 1549065600

2)

Brexit_data$year <-  Brexit_data$year[Brexit_data$weekday == '7'] - as.difftime(1, units = "days")
Brexit_data$year <-  Brexit_data$year[Brexit_data$weekday == '7'] -1

Which returned the following: Error in $<-.data.frame(*tmp*, year, value = c(1556755200, 1556755200, : replacement has 4206 rows, data has 29299*

So it can't for some reason not change the variables given the condition.

Stack overflow has always been for me a source of help for finding solutions. Hopefully someone can help me with this problem. Would help me a lot!

Danny
  • 1
  • (1) `ifelse` drops class, so if you want the result to be `POSIXct`, you need to convert *outside* of `ifelse`. Because of this, *this* part of your question is duplicate. (2) You have multiple questions, please don't do that. (3) Your second problem is "obvious" in a sense: you are subsetting on the right but not on the left of the assignment. Add your `[...]` to your `..$year[...] <-` on the left, too. – r2evans Mar 12 '20 at 14:49
  • @r2evans thanks for your answer. It helped! I also found an alternative here: https://stackoverflow.com/questions/6668963/how-to-prevent-ifelse-from-turning-date-objects-into-numeric-objects So for anyone with the same problem in the future, you can try the answer in the link above! – Danny Mar 12 '20 at 15:13
  • That link is literally in the "closed as duplicate" notice at the top of the question. – r2evans Mar 12 '20 at 15:18

0 Answers0