Formatting and converting column into datetime format in R? - r

I am fairly new to R and need help with applying operations to an entire column in a dataframe. Imagine a few values of the date_time column in the df look like this:
date_time
2017-05-01T00:00:00.000Z
2017-05-01T10:00:00.000Z
2017-05-01T20:00:00.000Z
...
Currently date_time is of type factor. If everything was formatted nicely, I think I want to do something similar to what I have below to convert it to DateTime (based off of what I've been seeing online):
df$date_time <- strptime(x = as.character(df$date_time), format = "%Y-%m-%d %H:%M:%S")
Does this look correct?
Assuming the code above is correct for converting the factor into DateTime, we still need to do some formatting for that to work. In order to use the code above, I have to get rid of the T that separates the date and time and replace that with ' ', and cut off the .000Z at the end. How can I do this for the entire column?
Thanks!

Related

Converting character to date in lubridate

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.

How to change dataframe R column from DD/MM/YYYY to YYYY/MM/DD without returning NAs

I have a dataframe that looks like this (see picture below). I want to change the date from DD/MM/YYYY to YYYY/MM/DD but for some reason it returns "NA" values! I think it has to do with the time values behind the date (I do not need those values).
The code I used was this (supposing DF is the data frame)
DF[,1] <- as.Date(DF[,1] , format = "%d-%m-%Y")```
Gregor Thomas gave me the answer: The format you show in the picture has slashes /, but the format string you use has dashes -. Try format = "%d/%m/%Y"

I can't convert an unknown column in to a date column and then change format to dd-mm-yyyy

I know it seems that this may be a repeated question, but I have tried other solutions and still cannot get it to work. I have uploaded a .csv file into r. I have done a small amount of house cleaning but ultimately I would like to convert a column from '"POSIXct" "POSIXt"' to a 'date' column type, and a 'character' column to a 'numeric'. For the latter column (change) I have decimals and --- entries, I converted the --- to NA, but fail to convert it to a 'numeric' afterwards.
df$value <- as.numeric(as.character(df$value))
I first used:
df$date <- dmy_hm(df$time_stamp, tz = "Europe/London")
to create a new date variable / column. But this did not give 'date' as a column type. I then tried using:
df$date <- as.Date(df$date)
but this did not work. Once I have converted to 'date' I need to convert the format from yyyy-mm-dd hh:mm:ss to dd/mm/yyyy.
Any help with will greatly received.
lubridate package can be wacky sometimes. Can you share head of you .csv data? you might have confused with dmy_hms with myd_hms or ymd_hms formats. Try using anytime package.
anytime::anytime(df$time_stamp)

Research panel analysis in R

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)

separate DateTime column in R

I have a datetime column in a data frame (named data) like this:
data$Date_Time
##[1] 14JUN2011:09:45:00
##[2] 15JUN2011:10:45:00
##[3] 16JUN2011:11:35:09
I'd like to obtain the date and time into separate columns.
For Date, it is easy to use:
data$Date <- as.Date(data$Date_Time, format("%d%b%Y"))
But I am having trouble to get the Time column.
You probably want to use the POSIXt class for the time, which can be parsed like:
data$Time <- strptime(data$Date_Time, "%d%b%Y:%H:%M:%S")
Also, I think that the date parsing should be:
data$Date <- as.Date(data$Date_Time, format="%d%b%Y")
In other words, don't call the format function. You're just lucky that works anyway.
you may use substr(date$Date_Time, 11, 18)

Resources