r - adding two column of a data where col1 contains date and col2 contains days


I have a data frame in which i have two columns date and days and i want to add date column with days and show the result in other column data frame-1 col date is in format of mm/dd/yyyy format

date        days
3/2/2019    8
3/5/2019    4
3/6/2019    4
3/21/2019   3
3/25/2019   7

and i want my output like this

date        days    new-date
3/2/2019    8       3/10/2019
3/5/2019    4       3/9/2019
3/6/2019    4       3/10/2019
3/21/2019   3       3/24/2019
3/25/2019   7       4/1/2019

i was trying this

as.Date("3/10/2019") +8

but i think it will work for a single value

1 Answer: 

Convert to actual Date values and then add Days. You need to specify the actual format of date (read ?strptime) while converting it to Date.

as.Date(df$date, "%m/%d/%Y") + df$days
#[1] "2019-03-10" "2019-03-09" "2019-03-10" "2019-03-24" "2019-04-01"

If you want the output back in same format, we can use format

df$new_date <- format(as.Date(df$date, "%m/%d/%Y") + df$days, "%m/%d/%Y")

#       date days   new_date
#1  3/2/2019    8 03/10/2019
#2  3/5/2019    4 03/09/2019
#3  3/6/2019    4 03/10/2019
#4 3/21/2019    3 03/24/2019
#5 3/25/2019    7 04/01/2019

If you get confused with different date format we can use lubridate to do

with(df, mdy(date) + days)

More Articles

cmake - How to build openCV 3.3.0 with GStreamer on Windows

Having Gstreamer 1.22 successfully installed I'm not able to configure the project to build OpenCV. CMake isn't able to find GStreamer on my machine. Any ideas how two address this issue?

regex - Regular expressions in R: pattern repetitions with {}

I am having trouble with a regular expression in R. The goal is to parse a Markdown/reST/knitr report text file in R to remove my own custom comments. These comments are put in the following form: Some sentence is about something <find a citation to this>.As Markdown uses <> for HTML tags,

android - Mock Location permission issue on release app

I developed Fake GPS application and all my codes work nicely on debug mode.When i try "mock location" on **release version**, i got this error;java.lang.SecurityException: Requires `ACCESS_MOCK_LOCATION` permissionI already had ACCESS_MOCK_LOCATION in debug/AndroidManifest file.<uses-permission

android - Why getLastKnownLocation(provider) doesn't return null after clearTestProviderLocation(provider)

I was assuming that getLastKnownLocation does return null for a given provider after calling clearTestProviderLocation for same provider.Why ? because documentation says for clearTestProviderLocation; Removes any mock location associated with the given provider.public void test() throws SecurityExc

php - Key for HMAC Algorithm

How to generate the secret key for the HMAC algorithm as I have to use it for data verification at the other clients end?Thanks in advance.

parsing - How to parse an array of json object using jq

I need to parse a Json file which have a lot of arrays. This is the json source:{"iabVersion": "IAB_V2","categories": [{ "categories": [{ "categories": [{ "id": "1.1.1", "name": "Commercial Trucks" }, { "id": "1.1.2", "name": "Conve

android - Exception while instantiating MockRunner- [NoSuchMethodError] [Kotlin]

I have created a library for distribution the network requests amongst different clients. I am using OkHttp3 in one of the clients. For testing my library, I am trying to mock the response using the okhttp3.mockwebserver. The way I am trying to set up my mock web server is as follows:val server = Mo

C++/C#/Java video analysis question

Could someone give references / frameworks, which allow to do something like Microsoft Kinect does, but using only one video stream?I would like to see algorithm abstracts and papers if there are some (I hope there are).If somebody had practical experience of using them, please share it :)

Streaming opencv Video

I need some ideas about how to stream video feed coming from opencv to a webpage. I currently have gStreamer, but I don't know if this is the right tool for the job. Any advice on using gStreamer or any hyperlinks to tutorials would be helpful and appreciated!Thanks!

iphone - Navigate Info Button to another screen Xcode

I have created an information button at the left side of the navigation bar.How can I allow users to navigate to another view named "AboutViewController" after clicking on the information button?Please help!! (Below is my codes)UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];[