Extract hour from a column of time [closed] - r

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Currently I have a column with time in the format yyyy-mm-dd hh:mm:ss, (eg. 2015-10-10 04:10:45) and I wish to extract the hour possibly using as.POSIXlt(x)$hour where x is my column.
Unfortunately, this function is returning a vector full of 0's, but if I do something like as.POSIXlt("2015-10-10 04:10:45")$hour I receive 4 which is what I want.
How can I do this with the whole column?

I was just doing the exact same thing on my dataset...
format(as.POSIXct(df$datetime, format="%Y-%m-%d %H:%M:%S"), format="%H:%M:%S")
#[1] "04:10:45"

Related

Dataframe convert factors to numerical error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to convert factors from a data-frame to numeric using the commands
data[] <- lapply (data, function(x) as.numeric(as.character(x))
But it keeps asking me for more coding. What am I doing wrong?
The data-frame is named data and it consists of 50 rows and 2 columns. Will this command change every variable in numeric right? Or shall I do something else?
screenshot after using 'dput' at http://imgur.com/Sde9QSk.png
Shouldn't you add ) at the end of your code?

convert to date in R using abbreviated month [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm new to R and trying to load some time series data but I'm stuck at the first hurdle.
I have a dataframe with a date column called Date. The date format of the data is: 23-May-16 (it appears like this in the R console when I print df). To read as date I'm trying:
df$Date <- as.Date(df$Date, "%dd-%bbb-%yy")
as per guidance here
which produces the value <NA> when it reads the data.
Try:
as.Date(df$Date,format="%d-%b-%y")
You only need to list those once:
as.Date("23-May-16", "%d-%b-%y")

How to calculate difference between dates in R [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am working on a project right now and encountered this problem.
I have a dataset consisting of two dates columns. One, say it x1, stands for check-in dates, the other, say it x2, stands for check-out dates.
Both of them are in the "year-month-day" format and have the type of string.
What I would like to do is figuring out how long does a person stay using check-in, check-out dates. I've tried multiple functions like as.Date. But all failed and I believe I just can't subtract these two dates directly as the results wouldn't represent the actual stay length.
Does anybody have any idea on how to do this in R?
Thanks!
If I understood your question, you want the difference between checkout and checkin? I would try this:
library(lubridate)
df<-data.frame(x1=c("2017-03-23","2017-03-24"),x2=c("2017-03-24","2017-03-28"))
df[]<-lapply(df,ymd)
df$x2-df$x1

Confusion About Date Conversion in R [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I know this has been asked here, but i did according to several answers but still received NA value, or value changed my year 2016 into 2020.
I need to convert factor into date. I tried as.Date function
Reporting_Raw_Data$Click_Date <- as.Date(Reporting_Raw_Data$Click_Date,"%d%m%y")
also tried:
Reporting_Raw_Data$Submit_Date <- as.Date(Reporting_Raw_Data$Submit_Date,format="%d-%m-%Y")
also:
Reporting_Raw_Data$Approve_Date <- as.Date(Reporting_Raw_Data$Approve_Date, format="%d-%m-%y")
I also tried POSIXct:
Reporting_Raw_Data$Click_Date <- format(as.POSIXct(strptime(Reporting_Raw_Data$Click_Date,
"%Y-%M-%D %H:%M:%S",tz="")),
format = "%Y-%M-%D")
also tried ymd_hms
Reporting_Raw_Data$Click_Date <- ymd_hms(Reporting_Raw_Data$Click_Date)
None gave me correct conversion.
sample table looks like below:
Click_Date
8/16/2016
8/7/2016
Maybe this could work:
Click_Date <- factor(c("8/16/2016",
"8/7/2016"))
strptime(as.character(Click_Date), "%m/%e/%Y")
This works fine for me:
as.POSIXct(x, format="%m/%d/%Y")
This is a data format knowledge issue.

Formatting data in R? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I format dates in R? I had to change something in my data set to account for blanks, and now my dates are very large negative numbers. I need to change them back into dates.
Assuming you have \t delimited input file.
Use the as.is argument to stop the read.table() function from converting the input variables. Then perhaps convert the date into something usable using strptime()
data <- read.table(file="...", sep="\t", as.is = TRUE)
data[,1] <- strptime(data[,1], "%Y-%m-%d")

Resources