How to extract month from a column in R [duplicate] - r

This question already has answers here:
How to extract month from a Year-Month (2017-10) type object in R?
(3 answers)
Closed 1 year ago.
I am trying to create a column which returns the month of a given observation.
My dataframe is called:" FF5_Class"
I tried to run the following code:
FF5_class$Month <- FF5_class %>%
month(date)
However, i received the following error message:"
"Error in as.POSIXlt.default(x, tz = tz(x)) :
do not know how to convert 'x' to class “POSIXlt”
A snip from my data is like this:
date Month
2000-01
2000-02
2000-03
2000-04
2000-05
2000-06
2000-07
2000-08
2000-09
2000-10
2000-11
2000-12
2001-01
2001-02
2001-03
2001-04
2001-05
2001-06
2001-07
2001-08
2001-09
2001-10
2001-11
2001-12
How can I do this? Thank you very much.

We can use couple of ways to do this. Regex option would be to remove the characters until the - with sub on the 'date' column
FF5_class$Month <- sub(".*-", "", FF5_class$date))
Or convert to a Date class after pasteing the day, then use format
FF5_class$Month <- format(as.Date(paste0(FF5_class$date, '-01')), '%m')
month works only a Date object and not on a character class

Related

R: how to extract full date from year week? [duplicate]

This question already has answers here:
Convert week number to date
(5 answers)
Closed 3 years ago.
I have a date in the follow format (year and week number):
year_week
201804
201938
201745
201402
201510
I need to get the full date in such format: 2018-02-26 (%Y-%m-%d).
I use the next code for this purpose:
dt[, full_date := format(ISOweek2date(sub("(\\d{4})(\\d{2})", "\\1-W\\2-1", year_week)),"%Y-%m-%d")]
but, I have the following error:
"Error: all(is.na(weekdate) | stringr::str_detect(weekdate, kPattern)) is not TRUE"
How can I get the full date in another way?
Why am I having this error?
Thanks a lot for any help!
P.S. Sunday or Monday can be used as the first day of week.
You can add a weekday to the date and use
as.Date(paste0(df$year_week, 1),"%Y%U%u")
#[1] "2018-01-29" "2019-09-23" "2017-11-06" "2014-01-13" "2015-03-09"
data
df <- structure(list(year_week = c(201804L, 201938L, 201745L, 201402L,
201510L)), class = "data.frame", row.names = c(NA, -5L))

R - Numeric(int) to Date conversion [duplicate]

This question already has answers here:
integer data frame to date in R [duplicate]
(3 answers)
Closed 4 years ago.
I have a date.frame and a column with
values(20180213190133, 20180213190136, 20180213190173 , 20180213190193 , 20180213190213, 20180213190233, 20180213190333, 20180213190533, 20180213190733, 20180213190833, 20180213190833, 20180213190833, 201802131901833, 20180213191133, 20180213192133, 20180213194133, 20180213199133, 20180213199133, 20180213199133, 20180213199133, 20180213190136.... 1200 entries)
I want to convert this column which is of type int to Date.
I tried using :
as.Date() and as.POSIXct(). Both doesn't work. I am getting N/A value.
Please let me how can I convert this filed from int to Date.
Thanks
Try this:
Input data
values<-c(20180213190133, 20180213190136, 20180213190173)
values_date<-as.Date(substr(as.character(values),start = 1,stop=8), format = "%Y%m%d")
> values_date
[1] "2018-02-13" "2018-02-13" "2018-02-13"
> class(values_date)
[1] "Date"
If you want to mantain also hour/minute/second you can try this:
values_date<-as.POSIXlt(as.character(values), format = "%Y%m%d%H%M%S")
After this the class will be "POSIXlt" "POSIXt" and not Date but there are some strange info in your input data
In the third number, last two figures are "73", this number is incorrect for seconds and you will have NA in output.
values_date
[1] "2018-02-13 19:01:33 CET" "2018-02-13 19:01:36 CET" NA

How to extract the hour of this kind of timestamp? [duplicate]

This question already has an answer here:
Extracting 2 digit hour from POSIXct in R
(1 answer)
Closed 7 years ago.
This is how my timestamp looks like
date1 = timestamp[1]
date1
[1] 07.01.2016 11:52:35
3455 Levels: 01.02.2016 00:11:52 01.02.2016 00:23:35 01.000:30:21 31.01.2016 23:16:18
When I try to extract the hour, I get an invalid 'trim' argument. Thank you !
format(date1, "%I")
Error in format.default(structure(as.character(x), names = names(x), dim = dim(x), :
invalid 'trim' argument
How can I extract single components like the hour from this timestamp.
With base R:
format(strptime(x,format = '%d.%m.%Y %H:%M:%S'), "%H")
[1] "11"
data
x <- as.factor("07.01.2016 11:52:35")
You first need to parse the time
d = strptime(date1,format = '%d.%m.%Y %H:%M:%S')
Then use lubridate to extract parts like hour etc.
library(lubridate)
hour(d)
minute(d)

About transforming dates in R 3.1.0 in OSX 10.9.2

I have a column of dates containing 2 different formats, that is DD/MM/YY and D/M/YY. Because the Microsoft Excel (for mac 2011, 14.3.9) recognised those dates labelled D/M/YY as M/D/YY in part of the variables, the output dates become incorrect.
Then I turned to R and tried to transform the column into a format of "DD-MON-YYYY", where MON is short form of months, like 01-Jan-2014. The column is something like this:
> head(date, 10)
date
1 17/12/96
2 27/6/07
3 21/6/13
4 24/7/13
5 17/7/13
6 16/7/13
7 13/10/99
8 20/2/97
9 14/12/96
10 19/6/13
I used the format function
format(date,"%d %b %Y")
And the output was
Error in format.default(structure(as.character(x), names = names(x), dim = dim(x), :
invalid 'trim' argument
I have also tried the lubridate package with no success.
> library(lubridate)
> dmy(date)
[1] NA
Warning message:
All formats failed to parse. No formats found.
Is there any simple method to transform the date?
You need to transform your strings to objects of class date, e.g.
as.Date("17/12/96", "%d/%m/%y")
[1] "1996-12-17"
and then apply your format
format(as.Date("17/12/96", "%d/%m/%y"), "%d-%b-%Y")
[1] "17-Dec-1996"

Create a proper date variable from an existing date variable in R

I have a data frame 'rta' with a date variable (date of death) with data entered in multiple formats like DD/MM/YY, D/M/YY, DD/M/YY, D/MM/YY, DD/MM, D/MM, D/M, DD/M.
rta$date.of.death<-c('12/12/08' ,'1/10/08','4/3/08','24/5/08','23/4','11/11','1/12')
Luckily all the dates belong to the year 2008.
I want to make this variable into a uniform format of DD/MM/YYYY, for example 12/12/2008. How to get it this way?
You could use this quick'n'dirty way:
rta <- data.frame(date.of.death=c('12/12/08' ,'1/10/08', '4/3/08',
'24/5/08','23/4','11/11','1/12'),
stringsAsFactors=F)
# append '/08' to the dates without year
noYear <- grep('.+/.+/.+',rta$date.of.death,invert=TRUE)
rta$date.of.death[noYear] <- paste(rta$date.of.death[noYear],'08',sep='/')
# convert the strings into POSIXct dates
dates <- as.POSIXct(rta$date.of.death, format='%d/%m/%y')
# turn the dates into strings having format: DD/MM/YYYY
rta$date.of.death <- format(dates,format='%d/%m/%Y')
> rta$date.of.death
[1] "12/12/2008" "01/10/2008" "04/03/2008" "24/05/2008" "23/04/2008" "11/11/2008" "01/12/2008"
Note:
this code assumes that no date has a four-digit year e.g. 01/01/2008
Try this:
as.Date(paste0(rta$date.of.death, "/08"), "%d/%m/%y")
giving
[1] "2008-12-12" "2008-10-01" "2008-03-04" "2008-05-24" "2008-04-23"
[6] "2008-11-11" "2008-12-01"

Resources