I am trying to convert a vector of factors into a vector of dates. The data is formatted as month/date (e.g. 5/20, 4/13, 11/11). I want to retain the format but need to change the data type from factor to date.
df$date <- as.Date(df$date, format = '%m/%d')
Is what I have at the moment. What is returned is 2017-5-20 or 2017-4-13 for example. My question is, is there a way to have as.Date not return the year? Additionally, is there a way to do this in lubridate that may be more efficient? I would like my output to return 5-20 or 4-13. All examples I find online always include the year and seem to leave my question unanswered.
Related
I have a character column of a df looking like:
a <- "6/10/21 19:34"
where the order is m/d/Y H:m.
I can't figure out why I am not able to convert it into datetime object.
I tried with:
mdy_hms(a,tz=Sys.timezone())
But I can't find a viable solution.
This question already has an answer here:
r - Filter a rows by a date that alters each day
(1 answer)
Closed 1 year ago.
I have a question about the use of a logical expression in combination with a variable.
Imagine that I have a data frame with multiple rows that each contain a date saved as 2021-09-25T06:04:35:689Z.
I also have a variable that contains the date of yesterday as '2021-09-24' - yesterday <- Sys.Date()-1.
How do I filter the rows in my data frame based on the date of yesterday which is stored in the variable 'yesterday'?
To solve my problem, I have looked at multiple posts, for example:
Using grep to help subset a data frame
I am well aware that this question might be a duplicate. However, current questions do not provide me with the help that I need help. I hope that one of you can help me.
As an initial matter, it looks like you have a vector instead of a data frame (only one column). If you really do have a data frame and only ran str() on one column, the very similar technique at the end will work for you.
The first thing to know is that your dates are stored as character strings, while your yesterday object is in the Date format. R will not let you compare objects of different types, so you need to convert at least one of the two objects.
I suggest converting both to the POSIXct format so that you do not lose any information in your dates column but can still compare it to yesterday. Make sure to set the timezone to the same as your system time (mine is "America/New_York").
Dates <- c("2021-09-09T06:04:35.689Z", "2021-09-09T06:04:35.690Z", "2021-09-09T06:04:35.260Z", "2021-09-24T06:04:35.260Z")
Dates <- gsub("T", " ", Dates)
Dates <- gsub("Z", "", Dates)
Dates <- as.POSIXct(Dates, '%Y-%m-%d %H:%M:%OS', tz = "America/New_York")
yesterday <- Sys.time()-86400 #the number of seconds in one day
Now you can tell R to ignore the time any only compare the dates.
trunc(Dates, units = c("days")) == trunc(yesterday, units = c("days"))]
The other part of your question was about filtering. The easiest way to filter is subsetting. You first ask R for the indices of the matching values in your vector (or column) by wrapping your comparison in the which() function.
Indices <- which(trunc(Dates, units = c("days")) == trunc(yesterday, units = c("days"))])
None of the dates in your str() results match yesterday, so I added one at the end that matches. Calling which() returns a 4 to tell you that the fourth item in your vector matches yesterday's date. If more dates matched, it would have more values. I saved the results in "Indices"
We can then use the Indices from which() to subset your vector or dataframe.
Filtered_Dates <- Dates[Indices]
Filtered_Dataframe <- df[Indices,] #note the comma, which indicates that we are filtering rows instead of columns.
I have a df with a column of the formate df$date = (1800.01, 1800.02, 1800.03) and so on.
And I can't figure out how to convert these numbers into proper monthly dates.
I tried the function date_decimal from the lubridate package, but that does not work for how my dates are formated.
Any ideas?
Probably the best way is to convert to a character first, then use one of the standard conversion functions. e.g.:
lubridate::ym(as.character(df$date))
I'm a Rookie with R. I have read in a Data Frame from Excel in R with the read.csv2 call, (Converted the Excel-file into csv).
I changed every Date in the table to a Y-M-D Format and wanted to use:
lapply(df$dates, as.Date, Format = "%Y/%m/%d")
but it produces NAs for every Date then.
When i ask for the mode it says the Dates are "numeric".
I tried to convert into character before into Dates with:
lapply(df$dates, as.character)
I dont know why it producs the NAs. Can someone help?
If you want to avoid the pain of finding the good format, there is dataPreparation package which provide a function to do that easily.
require(dataPreparation)
df <- setColAsDate(df, cols = "dates")
It will try to guess the format among thousand of various formats.
(NB: Please note that I'm the developer of this package.)
First, new to programming.
I built a table with 3 columns and I want to evaluate based on time series, so I'm playing around with the ts() function. The first column of my table is DATE as.date in the format "yyyy-mm-dd". I have one observation per variable per day. I've apply ts() to the table and tried start=1 (first observation?) and checked head(df) and the DATE column is sending back loose sequence of numbers that I can't identify (12591, 12592, 12593, 12594, 12597, 12598).
Could it be that the as.date is messing things up?
The line I use is:
ts(dy2, start=1, frequency= 1)
I've also been playing with the deltat argument. In the help file it suggests 1/12 for monthly data. Naturally, I tried 1/365 (for daily data), but have yet to be successful.
As suggested by G. Grothendieck you can use the zoo package. Try this:
require(zoo)
dates <- as.Date(dy2[,1], format = "%Y-%m-%d")
x1 <- zoo(dy2[,2], dates)
plot(x2)
x2 <- zoo(dy2[,3], dates)
plot(x1)
If this does not work, please provide further details about your data as requested by MrFlick. For example, print the output of dput(dy2) or at least head(dy2).