Date
01-2018
02-2018
01-2019
02-2019
I tried using arrange(df, Date)
It gets arranged as
01-2018
01-2019
02-2018
02-2019
Here is one base R option. We can try ordering the data frame using an on the fly date based on the text strings.
df <- data.frame(Date=c("01-2018", "02-2018", "01-2019", "02-2019"),
stringsAsFactors=FALSE)
df[order(as.Date(paste0("01-", df$Date), format="%d-%m-%Y")), ]
[1] "01-2018" "02-2018" "01-2019" "02-2019"
Note that I form a complete date by arbitrarily using the first of the month, for each text date, using as.Date with the correct format mask to generate a bona fide date.
For best results, consider storing your dates in a proper date column, or, if you must use text, use an ISO format which would at least sort properly.
Related
I have my date column formats in 01-Oct-21 and I want to change them to 01-10-2021. Does anyone knows how to do this in R?
Thanks!
Using base R. You might need to convert your original column into Date format using
old_date <- as.Date('01-Oct-21', format = '%d-%b-%y')
Then you use the format function to get into what you what
format(old_date, '%d-%m-%Y')
It will look slightly different if your dates are in a data frame.
We can use
library(lubridate)
format(dmy("01-Oct-21"), '%d-%m-%Y')
[1] "01-10-2021"
I am running some data pre-processing in Rstudio. There are two different kinds of date format in my data.frame. And I want to convert the numeric in the DOB column into %Y/%m/%d format like others. Could you please offer me some advice? Thanks in advance.
The nchar of that column will allow to distinguish the two different data formats. Then use the foramting to convert your date inputs into desired format.
This could be achieved using the tidyverse in one line
library(tidyverse)
my.data.frame%>%mutate(Date_new=ifelse(nchar(as.character(DOB))<5, format(as.Date(.$DOB, "%Y-%m-%d"), DOB)
Just a brief (and hopefully simple!) question;
I have a column of dates such as 08/14 and 08/15. However the column class is character, and I was wondering if it is possible (and if so how) to convert this into a date class? I've tried lots of different ways and it either does nothing, or returns N/A values for the whole column. Hopefully there should be a simple line of code to fix this that I haven't come across yet?
Any help much appreciated!!
One option is to create a day by pasteing and then use as.Date because date also needs day info
as.Date(paste0(c("08/14", "08/15"), "/01"), format = "%m/%y/%d")
#[1] "2014-08-01" "2015-08-01"
Or make use of as.yearmon from zoo
library(zoo)
as.Date(as.yearmon(c("08/14", "08/15"), "%m/%y"))
library(dplyr)
dates <- c("08/14", "10/13", "12/09") # year-month
datesWithYear <- paste(date, "/01", sep = "") %>%
as.Date()
If you want to use the year-month combination as a factor for cohort analysis of something similar you can always use as.yearmon() from the zoo package. tsibble and lubridate also have interesting functions for dealing with dates and time.
I am a newbie to Stackoverflow, stats and R, so apologies for the simple nature of my question/request for advice:
I am completing analysis of a large data-set comprising of 2 files: a txt containing internal temperature data and a second SPSS data file.
To kick off, I have exported the SPSS data into CSV format and stripped back to contain just the few columns i think i need - house type and occupant type. I have imported all the temperature data and merged the two using a common identifier.
So now I have a merged data frame, containing all the data i need (to begin with) to start completing some analysis.
First question: I have year, date and time as separate columns. However the time column has imported with an incorrect date before "30/12/1899". How can i delete the date part of all observations from this column, but retain the time?
Second question Similar to above, the date colum shows the correct date, but has the time following, which is not correct (every observation showing 00:00:00), how can I delete all the times from this column?
Third question How can I combine the correct Time with correct date, to end up with DD/MM/YYYY HH:MM:SS
Fourth question Should i create subsets of merged to facilitate the analysis: ie: each house type (seperate subsets) vs temp, time and occupant type?
Dates can be brought in as they are instead of factor via the parameter as.is = TRUE i.e.
data <- read.csv(choose.files(), as.is = T)
I would try reading the csv file again and then working with the date time. It will come in as a chron or some format like that and you'll need to change it to Posixct, well I do anyway. To view help on a function, type question mark followed by function name i.e. ?as.posixct.
Date.Time: chron "2018/08/04 10:10:00", ... # '%Y-%m-%d %H:%M:%S' current format as read in from my system.
# Date format you want is '%d/%m/%Y %H:%M'
# tz='' is an empty time zone can't remember exactly you probably should read up on
# finally on the left side of the assign <- I am creating a new column Date.
# You can over write the old column, Date.Time, but can't hurt to learn how to delete
# a column.
data$Date <- as.POSIXct(date$Date.Time, tz='', '%d/%m/%Y %H:%M:%S')
# Now remove the original column. -Date.Time take out Date.Time, if you leave the
# minus out, the data will contain the subset Date.Time and no other columns.
data <- subset(data, select = -Date.Time)
Try this first, and I will look into removing time with in a date field. I have an idea, but I'd rather see if this helps with the problem first.
Though if you do want to merge the Year, month, day columns, you could try something like this, seem like a logical thing to do, you can always keep the original format and delete it later. It's not hurting anything.
data$YMD <- paste(data$Year," ",
data$Month, " ",
data$Day)
Also while you are at it. Install a library called dplyr, written by the same guy that did ggplot2, Hadley....
install.packages("dplyr")
# The add it to the top of your file like ggplot.
library(dplyr)
I have two datasets which I want to match up based on date, but they have the dates formatted in different ways.
The two formats are:
9/11/2012
20120911
I know I can convert the first one to a standard format by doing the following:
# convert date info in format 'mm/dd/yyyy'
strDates <- c("01/05/1965", "08/16/1975")
dates <- as.Date(strDates, "%m/%d/%Y")
What can I do with the second date format to make it a standard type?
I tried this but it doesn't quite work:
as.Date(data$DATE[1],"%Y%m%d")