how to convert numeric only year in Date in R?
You could use round_date
, [EDIT] or better floor_date
to round to the start of the year(see comment & answer below):
library(lubridate)
df2 <- df %>%
mutate(date2 = dmy(date2)) %>%
mutate(year_internal = round_date(date2,'year')) %>%
mutate(year_out = format(year_internal,'%Y'))
df2 %>% select(date2, name, year_internal, year_out)
date2 name year_internal year_out
1 2000-01-01 A 2000-01-01 2000
2 2000-08-08 B 2001-01-01 2001
3 2001-03-16 C 2001-01-01 2001
Convert character date in R fread to show four digit year format
Maybe this could help
> strftime(as.Date(d,format = "%d-%b-%y"),format = "%d-%b-%Y")
[1] "12-Dec-2014" "19-Dec-2014" "12-Dec-2014" "19-Dec-2014" "12-Dec-2014"
[6] "26-Dec-2014"
Data
d <- c("12-Dec-14", "19-Dec-14", "12-Dec-14", "19-Dec-14", "12-Dec-14",
"26-Dec-14")
as.Date returns the day with four digits in R
The default format for Date
class is "%Y-%m-%d"
i.e. 4 digit year followed by a dash, followed by two digits, then a dash and two digits. Any other format, we need to specify the format
argument in as.Date
as.Date(mydate, "%d/%m/%y")
#[1] "2020-03-20"
We need to be very careful with two-digit year as it can lead some strange output. It is better to have 4 digit year
Problem with displaying reformatted string into a four-digit year in Stata 17
The value of the underlying data is still days from 1 Jan 1960 as you are using the date()
function. So keep %td
as you are working with days here, not years. But then you can decide for it to display only the year using %tdCCYY
C
standing for century and Y
for year. But remember, the underlying data point is still the day 1 Jan 2016 and not 2016
clear
input str4 school_year
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
end
gen school_year2 = date(school_year,"Y")
format %tdCCYY school_year2
list school_year school_year2 in 1/10
If year is all you want to work with then use the year()
function to get the year from the date. The examples below details steps you can play around with.
clear
input str4 school_year
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
"2016"
end
gen school_year2 = date(school_year,"Y")
gen school_year3 = year(school_year2)
format %tdCCYY school_year2
format %ty school_year3
list in 1/10
Note that in the last example, all values look the same to you. But the first variable is a string with the text "2016", the second is a date stored as the number of days from 1 Jan 1960 with only its year value displayed, and the last is a number with the number of years from year 0 displayed as a year (which in this case would have been the same had it been displayed as its underlying number).
Converting Numeric format to YYYY Date in R
There is no way of having a date object without including a day and a month.
Even if you write:
strptime(as.character(y), "%Y")
This include a day and a month:
strptime(as.character(1999), "%Y")
[1] "1999-10-07 EEST"
As a workaround you can try keeping the year as it is or including the first day for each year and extract the year whenever you want to, like this:
base-r:
date <- strptime("2012", "%Y")
format(date, "%Y")
[1] "2012"
as.numeric(format(date, "%Y"))
[1] 2012
lubridate:
require(lubridate)
year(date)
[1] 2012
Getting a date from a 4 digit strange date format in Oracle SQL
Split it up in multiple with clauses so it is easier to understand, you can join it into a single query if you want.
WITH sampledata (dt) AS
(
SELECT '0213' FROM DUAL UNION
SELECT '1212' FROM DUAL UNION
SELECT '2212' FROM DUAL UNION
SELECT '3212' FROM DUAL UNION
SELECT '4213' FROM DUAL UNION
SELECT '5213' FROM DUAL UNION
SELECT '6212' FROM DUAL UNION
SELECT '7212' FROM DUAL UNION
SELECT '8213' FROM DUAL UNION
SELECT '9212' FROM DUAL
), parsed_sampledata (yr, ddd) AS
(
SELECT substr(d.dt,1, 1) + TO_CHAR(SYSDATE,'YY') as yr, substr(d.dt,2,3) as ddd
FROM sampledata d
)
SELECT TO_DATE(ddd||yr - (CASE WHEN yr - TO_CHAR(SYSDATE,'YY') < 5 THEN 0 ELSE 10 END),'DDDYY')
FROM parsed_sampledata d;
31-JUL-2020
31-JUL-2021
31-JUL-2022
31-JUL-2023
31-JUL-2024
01-AUG-2015
30-JUL-2016
31-JUL-2017
01-AUG-2018
31-JUL-2019
Convert two digit year to four digits and also support one or two digit month
You can use YearMonth
for this like so:
final DateTimeFormatter YEAR_FORMAT = DateTimeFormatter.ofPattern("[yyyy][yy]");
YearMonth yearMonth = YearMonth.of(
Year.parse(year, YEAR_FORMAT).getValue(),
month);
Note: The year should be a String
Outputs:
2020-12
2030-02
2041-05
Related Topics
Replace Values in a Vector Based on Another Vector
Merge Data Frames Based on Rownames in R
Delete Columns/Rows with More Than X% Missing
Displaying a Greater Than or Equal Sign
Get_Map Not Passing the API Key (Http Status Was '403 Forbidden')
R Package Lattice Won't Plot If Run Using Source()
Factors in R: More Than an Annoyance
Check Whether Values in One Data Frame Column Exist in a Second Data Frame
Rvest Error in Open.Connection(X, "Rb"):Timeout Was Reached
Command to See 'R' Path That Rstudio Is Using
R Function with No Return Value
Dplyr: Lead() and Lag() Wrong When Used with Group_By()
How to Multiply Data Frame by Vector
Emulate Split() with Dplyr Group_By: Return a List of Data Frames
Struggling with Integers (Maximum Integer Size)