0

I have a data frame and one of the columns is a date data type (telling in with year-month-date-hour-minute-second on offer was bought) and another column showing the validity of all my offers (for example 1-3-7-30 etc. days). How can i find out the date all my offers bought expire in a new column?

MrFlick
  • 195,160
  • 17
  • 277
  • 295
Beck
  • 115
  • 6
  • 4
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 12 '19 at 21:50
  • Ok, thank you. So I have a column called Validity that has this first 7 values that show for how many days different telephone bundles are valid to be used for customers: Validity 30 30 999 30 30 30 30 30 7 And another column called Date for example 2019-10-30 13-45-34 2019-10-27 13-30-30 and so on showing the date customers bought a telephone bundle. Now I want a new column that shows the date all the bundles are now valid anymore with the same data type as above y-m-d h-m-s – Beck Nov 12 '19 at 22:02
  • 1
    Rebeka, it's generally better to edit your question and add amplifying information like that, so that when somebody sees this question for the first time, they get everything up front. Comments can be both ignored (too many disparate places to read) and even hidden (when there are too many comments). – r2evans Nov 12 '19 at 22:12
  • I can only post one question a day because I am new but thank you for the suggestion – Beck Nov 12 '19 at 22:16

1 Answers1

0

Here is a mock example that you want:

library(dplyr)
x <- data.frame(
  date = seq.POSIXt(as.POSIXct("1970-01-01 00:00:00"),
                    as.POSIXct("1970-09-01 00:00:00"), 
                    by = "month"),
  validity = c(30,30,999,30,30,30,30,30,7))
y <- x %>% 
  mutate(expire = date + validity*60*60*24)

Output:

> y
        date validity              expire
1 1970-01-01       30 1970-01-31 00:00:00
2 1970-02-01       30 1970-03-03 00:00:00
3 1970-03-01      999 1972-11-23 23:00:00
4 1970-04-01       30 1970-05-01 00:00:00
5 1970-05-01       30 1970-05-31 00:00:00
6 1970-06-01       30 1970-07-01 00:00:00
7 1970-07-01       30 1970-07-31 00:00:00
8 1970-08-01       30 1970-08-31 00:00:00
9 1970-09-01        7 1970-09-08 00:00:00

This is what you expect to get but here I am not sure if 999 you provided is actual days or missing value. Since we do not have your data, we assume it is in POSIXct format and if it's not, then convert it into POSIXct and do the above.

Vitali Avagyan
  • 1,193
  • 1
  • 7
  • 17
  • Thank you very much for this but I don't want to know how much time I have left before a bundle expires, I want to know the date each observation (bundle brought) expires. For example if i buy one today 2019-11-12 23:14:11 with validity 7 days it expires in 2019-11-19 23:14:11. And 999 is actual days – Beck Nov 12 '19 at 22:14
  • Yes, that's what `expire = date + validity*60*60*24` does -- finds the expiration date. – Vitali Avagyan Nov 12 '19 at 22:18
  • Ok thank you so much :) I had only seen the zero seconds at the end and got confused – Beck Nov 12 '19 at 22:20