How to change given date in R? [duplicate] - r

This question already has answers here:
How to subtract/add days from/to a date?
(3 answers)
Closed 2 years ago.
I started working in R and need to change a date. On my search I only found how to change the format or setting the day/month/year to a specific number.
I have for example:
my_date = as.Date("2020-03-01")
in my algorithm I need to change this by one day. For this special case I am looking for
2020-02-29
Thanks for your answers.
Best
Felix

I would propose to use lubridate library.
Then:
library(lubridate)
date <- ymd("2020-03-01")
date - days(1)
gives you "2020-02-29".

Related

How to parse duration with moment js? [duplicate]

This question already has answers here:
Add hours minutes with the format hh:mm using moment
(5 answers)
How to convert time (hh:mm) to decimal form with momentjs
(1 answer)
Closed 3 years ago.
I have a duration in the format 'HH:mm'
for instance: 14:45 means 14hours and 45 seconds.
Does moment has a built function to parse duration based on a specific format ?

Date Time Formatting not working properly in ASP.NET [duplicate]

This question already has answers here:
C# DateTime to "YYYYMMDDHHMMSS" format
(18 answers)
Closed 5 years ago.
DateTime.Now.ToString("DD.MMM.YYYY.HH.MM.SS")
Using the above formatting to get the current date and time as per the above format. But it's resulting
DD.Sep.YYYY.15.09.SS
Use This it will definetly work as per your expecting output
string h = DateTime.Now.ToString("dd.MMM.yyyy.HH.MM.ss");

How to retrieve the weekday's date in R? [duplicate]

This question already has answers here:
Find the day of a week
(7 answers)
Closed 7 years ago.
Is there one function returning the weekday's date? For example, today is 3/25/2015. If input the parameter is current week(0) and weekday is Monday(1), it will return '3/23/2015'. If input is last week(-1) and weekday is Monday(1), it will return '3/16/2015'.
I believe as.posix allows you to request $wday

Using a variable to extract information from a dataframe in R [duplicate]

This question already has answers here:
Dynamically select data frame columns using $ and a character value
(10 answers)
Closed 8 years ago.
I apologize since I'm sure this is an obvious issue, but I just can't seem to find the correct search terms to come up with the answer.
If I have a dataframe like this:
example<-cbind(rbind(1,2,3),rbind("a","b","c"))
colnames(example)<-c("a","b")
example<-as.data.frame(example)
And I want to extract the values from column a using a variable x,
x<-a
How do I go about this? When I try:
example$x
I get a null. How do I make this work?
I'm assuming a is a character:
x <- "a"
example[,x]

How do I convert a string into date and day in R

I have a dataset with 10 columns, one of which is date in the following format
10-MAR-12 00.00.00.000000000
I would like to convert this into a data format which is read as a date and not as a string in the following format
10/03/12
I would also like there to be an additional column that says what day of the week it is
I would then like to filter out certain days or dates and to create a subset of my data.
I am a beginner to R so any help is appreciated
Take a look at ?strptime for formatting options and as.Date or as.POSIXct for the function to convert. Also, don't be surprised if your question is down voted or closed since this is a common question and answers can be found on SO or from quick google searching.
Specifically:
format(as.Date(tolower('10-MAR-12 00.00.00.000000000'), format='%d-%b-%y'), format='%d/%m/%y')
should give you the formatting you're looking for. If you want a date type though you should take off the outer format.

Resources