Change factor into time format - r

A column contains only time in H:M:S(e.g. 13:08:20) but its in FACTOR format so now I want to change the FACTOR into POSIXct so that I can apply ceiling date() function on it.
I have tried these in some cases when I run its shows no error but then the columns whole contains NA values. :
x <- anytime(cam5$CaptureTime)
x <- hms(cam5$CaptureTime)
x <- hms(as.character(cam5$CaptureTime))
x <- as.POSIXct(cam5$CaptureTime)
x <- as.POSIXct(as.character(cam5$CaptureTime))

We can use as.POSIXct and specify the format
as.POSIXct("13:08:20", format = "%T")
Or specifying it separately
as.POSIXct("13:08:20", format = "%H:%M:%S")
This would also work with strptime
strptime("13:08:20", format = "%T")

We can use hms from lubridate
library(lubridate)
hms("13:08:20")

Related

Convert character to date format and then compute difference in days

I know this question has probably been answered in different ways, but still struggling with this. I am working with a dataset where the dates format for date1 is '2/1/2000', '5/12/2000', '6/30/2015' where the class() is character. And the second column of dates date2 in the format '2015-07-06', '2015-08-01', '2017-10-09' where the class() is "POSIXct" "POSIXt" .
I am attempting to standardize both columns so I can compute the difference in days between them using something like this
abs(difftime(date1 ,date2 , units = c("days")))
I have tried numerous ways in converting the first date1 into the same class using strtime, lubridate etc. What's the best way to move forward for me to be able to standardize both and compute the difference in days?
sample data
x <- c('2/1/2000', '5/12/2000', '6/30/2015')
y <- as.POSIXct(c('2015-07-06', '2015-08-01', '2017-10-09'))
code
#make both posixct
x2 <- as.POSIXct(x, format = "%m/%d/%Y")
abs(x2 - y)
# Time differences in days
# [1] 5633.958 5559.000 832.000

Convert columns to posixct using sapply and keeping datetime format in R

I want to use sapply (or something similar) to convert certain columns to POSIXct in an R data.frame but maintain the datetime format of the columns. When I do it currently, it converts the format to numeric. How can I do this? An example is below.
#sample dataframe
df <- data.frame(
var1=c(5, 2),
char1=c('he', 'she'),
timestamp1=c('2019-01-01 20:30:08', '2019-01-02 08:27:34'),
timestamp2=c('2019-01-01 12:24:54', '2019-01-02 10:57:47'),
stringsAsFactors = F
)
#Convert only columns with 'timestamp' in name to POSIXct
df[grep('timestamp', names(df))] <- sapply(df[grep('timestamp', names(df))], function(x) as.POSIXct(x, format='%Y-%m-%d %H:%M:%S'))
df
var1 char1 timestamp1 timestamp2
1 5 he 1546392608 1546363494
2 2 she 1546435654 1546444667
Note: I can use as.posixlt instead of as.posixctand it works, but I want the data in POSIXct format. I also tried converting to POSIXlt first and then to POSIXct, but that also ended up converting the columns to numeric.
Use lapply rather than sapply. The "s" in sapply is for simplify and it turns the result into a matrix but sapply can't create a matrix of POSIXct values so it gets cast to a simple numeric matrix. But if you keep it a list, you don't lose the class.
df[grep('timestamp', names(df))] <- lapply(df[grep('timestamp', names(df))], function(x) as.POSIXct(x, format='%Y-%m-%d %H:%M:%S'))
You could also do this fairly easily with dplyr
library(dplyr)
df %>% mutate_at(vars(contains("timestamp")), as.POSIXct)

Convert number that is in factor type to date [duplicate]

So, I have a data.frame with a column called Date.birth, but I have these values in a numeric format:
Date.birth
43067
43060
Probably is problem format. But I need in a Date format like these:
Date.birth
11/28/17
11/21/17
Actually the above format is the correct. I tried this command:
as.Date(levels(data$Date.birth), format="%d.%m.%Y")
but didn't work. Anyone has a suggestion?
Thanks.
We need to specify the origin if it is a numeric value
as.Date(data$Date.birth, origin = "1899-12-30")
e.g.
as.Date(43067, origin = "1899-12-30")
#[1] "2017-11-28"
After converting to Date class, if it needs to be in a custom format, use format
format(as.Date(43067, origin = "1899-12-30"), "%m/%d/%y")
#[1] "11/28/17"
If your column is factor, do convert to numeric first
as.Date(as.numeric(as.character(data$Date.birth)), origin = "1899-12-30")
If this is an excel numeric date, janitor has a great solution:
library(janitor)
excel_numeric_to_date(data$Date.birth)
It can be simply done by using lubridate package-
lubridate::as_date(as.numeric(dt$Date.birth),origin="1899-12-30")
[1] "2017-11-28" "2017-11-21"
Sample Data-
dt <- read.table(text="Date.birth
43067
43060",header=T)
try this
VariableName <- dt %>%
mutate(Date.birth = format(excel_numeric_to_date(as.numeric(Date.birth)),"%m/%d/%y"))
#[1] "11/28/17"

How to convert a numeric value into a Date value

So, I have a data.frame with a column called Date.birth, but I have these values in a numeric format:
Date.birth
43067
43060
Probably is problem format. But I need in a Date format like these:
Date.birth
11/28/17
11/21/17
Actually the above format is the correct. I tried this command:
as.Date(levels(data$Date.birth), format="%d.%m.%Y")
but didn't work. Anyone has a suggestion?
Thanks.
We need to specify the origin if it is a numeric value
as.Date(data$Date.birth, origin = "1899-12-30")
e.g.
as.Date(43067, origin = "1899-12-30")
#[1] "2017-11-28"
After converting to Date class, if it needs to be in a custom format, use format
format(as.Date(43067, origin = "1899-12-30"), "%m/%d/%y")
#[1] "11/28/17"
If your column is factor, do convert to numeric first
as.Date(as.numeric(as.character(data$Date.birth)), origin = "1899-12-30")
If this is an excel numeric date, janitor has a great solution:
library(janitor)
excel_numeric_to_date(data$Date.birth)
It can be simply done by using lubridate package-
lubridate::as_date(as.numeric(dt$Date.birth),origin="1899-12-30")
[1] "2017-11-28" "2017-11-21"
Sample Data-
dt <- read.table(text="Date.birth
43067
43060",header=T)
try this
VariableName <- dt %>%
mutate(Date.birth = format(excel_numeric_to_date(as.numeric(Date.birth)),"%m/%d/%y"))
#[1] "11/28/17"

How can I reformat a series of dates in a vector in R

I have vector of dates that i'm trying to convert with as date but i'm not getting the expected output, when I sapply with as.Date instead of getting a series of reformatted dates I get the names as dates and some odd value.
dates = c("20-Mar-2015", "25-Jun-2015", "23-Sep-2015", "22-Dec-2015")
sapply(dates, as.Date, format = "%d-%b-%Y")
20-Mar-2015 25-Jun-2015 23-Sep-2015 22-Dec-2015
16514 16611 16701 16791
I would like each of the values in the vector to be showing the new formated value. E.g. like what would happen if as.Date was shown applied to each element
as.Date("20-Mar-2015", format = "%d-%b-%Y")
[1] "2015-03-20"
You can directly use as.Date(dates, format = "%d-%b-%Y"). as.Date is vectorized, i.e. it can take a vector as input, not only a single entry.
In your case:
dates <- c("20-Mar-2015", "25-Jun-2015", "23-Sep-2015", "22-Dec-2015")
as.Date(dates, format = "%d-%b-%Y")
# [1] "2015-03-20" "2015-06-25" "2015-09-23" "2015-12-22"

Resources