Retrieving date from sequence does not give the value [duplicate] - r

This question already has answers here:
Looping over a Date or POSIXct object results in a numeric iterator
(7 answers)
Closed 9 years ago.
I'm trying to run the following code in R
> mySeq <- seq(as.Date("2012-1-1",format = "%Y-%m-%d"),
as.Date("2012-1-3",format = "%Y-%m-%d"),
by="1 day")
> for (i in mySeq){print(i)}
And I get:
[1] 15340
[1] 15341
[1] 15342
but mySeq[1] returns "2012-01-01"
Why? what am I missing here?

Your date values are converted to numeric in for() function to use them as index values.
Instead you can use seq_along() to get index values and then print mySeq[i].
for(i in seq_along(mySeq)) {print(mySeq[i])}

Related

How convert numeric value to date in R (same as Short date in excel)? [duplicate]

This question already has answers here:
How to convert Excel date format to proper date in R
(5 answers)
Closed 2 years ago.
I want to convert numeric date format to date. I.E. I have value 44109, but want to convert it to 2020-10-06. How is it possible?
Try something like below
> as.Date(44109,origin = "1900-01-01")
[1] "2020-10-07"
this works as well
x <- as.POSIXct(44109 * (60*60*24), origin="1899-12-30", tz="GMT")
"2020-10-05 GMT"

How to convert a numeric form to date form [duplicate]

This question already has answers here:
Convert numeric to date
(2 answers)
Closed 3 years ago.
I am trying to convert the field which is numeric in my dataset df to a date format using R.
The value is present as sear_date in the dataset and is in format "20181212".
However, I would like to convert this field from numeric to date in R
df$search_date=as.Date(df$search_date,origin = "1970-01-01"))
I expect the output to just change the type of the variable from numeric to date. However, it changes the fields themselves to wrong data
Use as.Date with the indicated format. If it is numeric convert to character first.
as.Date("20181212", format = "%Y%m%d")
## [1] "2018-12-12"
as.Date(as.character(20181212), format = "%Y%m%d")
## [1] "2018-12-12"

%b-%Y date conversion gives NA [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 4 years ago.
I am trying to convert character strings to Dates in R. These are examples of the character strings:
"Aug-1973" "Aug-1974" "Aug-1975" "Aug-1976" "Aug-1977"
I run the following line on date strings similar to the ones above:
exportsDF$Date <- as.Date(as.character(exportsDF$Date), format = "%b-%Y")
This returns NAs for all values. The step where I convert the dates column to characters returns the correct values. Any ideas why the as.Date() command is not working? There are no NAs or missing values in the data. Every value has a "%b-%Y" format.
Any help is appreciated!
The date format needs a day as well, so you could add an arbitrary day of the month. Here, I've chosen the first day:
dates <- c("Aug-1973", "Aug-1974", "Aug-1975", "Aug-1976", "Aug-1977")
res <- as.Date(paste0("01-", dates), format = "%d-%b-%Y")
print(res)
#[1] "1973-08-01" "1974-08-01" "1975-08-01" "1976-08-01" "1977-08-01"
The reason is that the underlying Date data type is an integer counting the days since some reference day. Specifically, the number of days since 1970-01-01. See ?Date.
The Date object res can now be displayed as you please via
format(res, "%B-%Y")
#[1] "August-1973" "August-1974" "August-1975" "August-1976" "August-1977"
or similar.
The month(res) function and its cousins are also helpful. See ?month.

Convert string to data format [duplicate]

This question already has an answer here:
R convert character vector values to Date values
(1 answer)
Closed 7 years ago.
I have a large number of strings of the following format:
a <- "19260701"
I would like to convert these do the following date format:
1926-07-01
when I try:
as.Date(a, "%Y-%m-%d")
I get
NA
You need to put the format of a in the function as.Date() :
a <- "19260701"
as.Date(a,format="%Y%m%d")
[1] "1926-07-01"
a<- "19260701"
library(lubridate)
ymd(a)
[1] "1926-07-01 UTC"

rounding times to the nearest hour in R [duplicate]

This question already has an answer here:
Round a POSIX date and time (posixct) to a date relative to a timezone
(1 answer)
Closed 9 years ago.
I have data in the format
time <- c("16:53", "10:57", "11:58")
etc
I would like to create a new column where each of these times is rounded to the nearest hour. I cannot seem to get the POSIX command to work for me.
as.character(format(data2$time, "%H:%M"))
Error in format.default(structure(as.character(x), names = names(x), dim = dim(x), :
invalid 'trim' argument
Let alone use the round command. Can anyone advise?
## Example times
x <- c("16:53", "10:57", "11:58")
## POSIX*t objects need both date and time specified
## Here, the particular date doesn't matter -- just that there is one.
tt <- strptime(paste("2001-01-01", x), format="%Y-%m-%d %H:%M")
## Use round.Date to round, then format to format
format(round(tt, units="hours"), format="%H:%M")
# [1] "17:00" "11:00" "12:00"

Resources