Converting string dates to numeric dates - r

I want to convert this kind of dates :Apr 09, 2019 to this kind of dates: Apr 09, 2019-04-09
I wrote
as.Date(Data$date, format = "%B %d, %Y")
format(as.Date(Data$date, format = "%B %d, %Y"), "%d-%m-%Y")
That code worked, however when I View(Data) I see that it had not converted.
Why? Any idea?

The reason is that the column is not updated. We need to assign (<-) the results back to the original column or a new column
Data$date <- format(as.Date(Data$date, format = "%B %d, %Y"), "%d-%m-%Y")

Related

Convert to date from an unusual format [duplicate]

folk!
R can not recognize the next type of date: "Jun 3, 1986"
I tried it several times converting that column to factor or character.
The next examples from stackoverflow do not work and return "NA":
sdate2 <- "jan151999"
ndate2 <- as.Date(sdate2, "%B%d%Y"); ndate2
dates <- c("May 27 1984", "July 7 2005")
betterDates <- as.Date(dates,
format = "%B %d %Y")
I used small letter %b and it did not work also.
My version does not work as well
as.Date("Jun 3, 1986", format = "%b %d, %Y")
Could you help me?
That is probably because your locale language is different. Change it to English and it should work.
Sys.setlocale("LC_ALL","English")
as.Date("Jun 3, 1986", format = "%b %d, %Y")
#[1] "1986-06-03"
And also this way works well. SOme oone suggested it but deleted afterward.
lubridate::mdy("Jun 3, 1986")

in R, modify date from aug 07, 2020 to 08,07,2020, also how to remove time zone

[Fri Aug 07, 2020 05:12 UTC]
I have this date format in a column, how to modify it to be 08, 07,2020 05:12
also, how to remove UTC from all columns
Check ?strptime for various format options. First convert the data to POSIXct, you can then use format to get it any format that you want.
x <- 'Fri Aug 07, 2020 05:12 UTC'
x1 <- as.POSIXct(x, format = '%a %b %d, %Y %H:%M UTC', tz = 'UTC')
x1
#[1] "2020-08-07 05:12:00 UTC"
format(x1, '%m,%d,%Y %H:%M')
#[1] "08,07,2020 05:12"
If we want to apply this for multiple columns we can use lapply. For example for first 4000 columns where your dataframe is called df we can do :
cols <- 1:4000
df[cols] <- lapply(df[cols], function(x) format(as.POSIXct(x,
format = '%a %b %d, %Y %H:%M UTC', tz = 'UTC'), '%m,%d,%Y %H:%M'))

R can not recognize date in format

folk!
R can not recognize the next type of date: "Jun 3, 1986"
I tried it several times converting that column to factor or character.
The next examples from stackoverflow do not work and return "NA":
sdate2 <- "jan151999"
ndate2 <- as.Date(sdate2, "%B%d%Y"); ndate2
dates <- c("May 27 1984", "July 7 2005")
betterDates <- as.Date(dates,
format = "%B %d %Y")
I used small letter %b and it did not work also.
My version does not work as well
as.Date("Jun 3, 1986", format = "%b %d, %Y")
Could you help me?
That is probably because your locale language is different. Change it to English and it should work.
Sys.setlocale("LC_ALL","English")
as.Date("Jun 3, 1986", format = "%b %d, %Y")
#[1] "1986-06-03"
And also this way works well. SOme oone suggested it but deleted afterward.
lubridate::mdy("Jun 3, 1986")

Getting a handle on date conversion in R

I created a column of dates by hand.
Need to convert entire column Date to xx/xx/xxxx (01/dd/2012)
I know how to use substrings to extract month day year from a standard data class like 01JAN12
DateM=substr(Date,START,FINISH)
DateM
Now put it together as xx/xx/xx
Here is my code:
Date=c("January 23,2012","January 24,2012","January
25,2012","January 26,2012","January 27,2012")
# WANT: 23JAN12 24JAN12 25JAN12 26JAN12 27JAN12
Date3=format(Date,"%B %d %Y")
# Error in format.default(Date, "%B %d %Y") : invalid 'trim'
argument but I still get the dates ok when I print out
#WANT: 01/23/2012 01/24/2012 01/25/2012 01/26/2012 01/27/2012 from
Date3
Date4=format(Date3,"%B %d %Y")
invalid 'trim' argument
# WANT: 2012-01-23 2012-01-24 2012-01-25 2012-01-26 2012-01-27
Date5=format(Date3,"%Y %B %d")
invalid 'trim' argument
I want to use the basic functions of R as opposed to lubridate.
Can someone direct me how to finish this, please?
You should use as.Date and not format
as.Date(Date, "%B %d, %Y")
#[1] "2012-01-23" "2012-01-24" "2012-01-25" "2012-01-26" "2012-01-27"
as.Date is used to convert dates from different character representation to "Date" class.
Once you have data in the "Date" class, we can then use format to represent them in the way we want.
format(as.Date(Date, "%B %d, %Y"), "%m/%d/%Y")
#[1] "01/23/2012" "01/24/2012" "01/25/2012" "01/26/2012" "01/27/2012"

How you convert a character date (February 12, 1931) to date format (Y,m,d)

I'm having a problem converting a data frame column from whatever format it read the txt file as to a date format.
movie_df <- read.csv("scary_movies.txt",
stringsAsFactors = FALSE,
na.strings = c("NA", ""),
sep = "\t")
This is how we read the file.
There's a column called ReleaseDate that has the date in this format: February 12, 1931. How could I change it to 1931/2/12.
If I use strptime() the column shows up as NA.
An option would be to convert to Date class with as.Date and format it to the required format
movie_df$ReleaseDate <- format(as.Date(movie_df$ReleaseDate,
"%B %d, %Y"), '%Y/%m/%d')
with a reproducible example
format(as.Date('February 12, 1931', "%B %d, %Y"), '%Y/%m/%d')
#[1] "1931/02/12"
If the leading 0's for days/months needs to be dropped
sub("^(\\d{4})-0?(\\d{1,2})-0?(\\d{1,2})$", "\\1/\\2/\\3",
as.Date('February 12, 1931', "%B %d, %Y"))
#[1] "1931/2/12"

Resources