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

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))

Related

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

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

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

R + Adding n number of weeks to YYYYWW format

I am trying to add 52 weeks to a date variable which is in YYYYWW format. my initial date is 201616 (year 2016 and week 16) and i am trying to add 52 weeks to this date and the expected output is 201715.
I tried couple of things but no luck, here is what i tried so far
date <- as.Date(as.character(201616), "%Y%W")
seq(date, by = "1 week", length.out = 52)
I would greatly appreciate your input. Many Thanks for your time!
The problem is that there are 7 days in week #16 2016. You need to specify a day to convert it to a date that can be used to add days. In the code below %u indicates first day of the week. You can then add 52 weeks to this number.
date1 <- as.Date("201616 1", format = "%Y%U %u")
format(date1+(52*7), "%Y%U")
[1] "201716"
I'm not sure that as.Date can take %Y%W and generate a unique value. It appears to be populating date with the current month and day. If instead we specify a date in the 16th week:
date <- as.Date("2016-04-23")
and format that in your style
format(date, "%Y%W")
[1] "201616"
we can generate a sequence of 52 values from this
newdate_seq <- seq(date, by = "1 week", length.out = 52)
and change those to your format too
format(newdate_seq, "%Y%W")
[1] "201616" "201617" "201618" "201619" "201620" "201621" "201622" "201623" "201624" "201625" "201626" "201627"
[13] "201628" "201629" "201630" "201631" "201632" "201633" "201634" "201635" "201636" "201637" "201638" "201639"
[25] "201640" "201641" "201642" "201643" "201644" "201645" "201646" "201647" "201648" "201649" "201650" "201651"
[37] "201652" "201701" "201702" "201703" "201704" "201705" "201706" "201707" "201708" "201709" "201710" "201711"
[49] "201712" "201713" "201714" "201715"
which ends where you expect.
FYI, for next time, try highlighting what caused you to think there was "no luck" -- what errors did you produce, what results did you produce and how did they differ from what you expect to produce? Simply printing the date variable showed me that it wasn't doing what you expected.

week-dates in R [duplicate]

This question already has answers here:
Transform year/week to date object
(2 answers)
Closed 4 years ago.
In R, how do I convert the string 1/2010 (week 1 of 2010) into a Date or POSIXct (or POSIXlt) object?
I tried
as.Date('1/2010', "%W/%Y")
[1] "2010-06-29"
I also tried
strptime('1/2010', "%W/%Y")
[1] "2010-06-29 BRT"
But these are clearly not what I want.
In the end, I guess doesn't really matter which exact is picked, so long as I can correctly re-convert this to "weeks since origin".
library(splitstackshape)
date <- c("1/2013","3/2013")
date = data.frame(date)
df = data.frame(cSplit(date,"date","/"))
colnames(df) = c("week", "year")
df$date = as.Date(paste(df$year, df$week, 1, sep="-"), "%Y-%U-%u")

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)

Resources