Confusion About Date Conversion in R [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
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.

Related

Change the date format in R [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 2 years ago.
Improve this question
Is there a way to change the date format in R. Example, when I tried with below formula to convert, it was not showing a right result
format(as.Date("30-01-2021"), "%Y/%d/%m")
"0030/20/01"
Expected output
"2021-01-30"
The lubridate package works really well for this kind of formatting.
#Load library
library('lubridate')
# Convert date format
lubridate::dmy("30-01-2021")
The reason it is not working is because your date string is not in the default date format. The default is "%Y-%m-%d" but your string is "%d-%m-%Y". So you can fix it by specifying the input date format on the as.Date() function. Like this:
as.Date("30-01-2021", "%d-%m-%Y")
# [1] "2021-01-30"

How do I convert a column of dates in integer format to date format in base R? [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 2 years ago.
Improve this question
I'm working with a dataframe that has a column of dates, all of which are integers in the form 20210117 for January 17, 2021.
I tried to run
data$DATE <- as.Date(as.character(data$DATE), "%Y%m%d")
but this turns all my dates into NAs. What am I doing wrong and is there a way to fix this in base R without importing libraries?
You can use the format option when calling as.Date:
dates <- as.character(data$YearMonthDay)
data$newVar = as.Date(dates, format="%Y%m%d")
Note that you don't need to use the origin parameter here, because even though you are passing in numerical data, it is in the form of an actual text date. origin is used when passing in number of days since an epoch.
It is generally not advisable to go in this direction, and in any case, base R date's have to have a year component. So, include the year component, and if you don't want to use it, then don't use it.

Rounding all numbers in r with no decimals [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 3 years ago.
Improve this question
I'm just starting out in R and I'm trying to round all the numbers with no decimals in my data frame, preferably when I'm reading in a file.
I've looked up several ways to round decimals, but it is not clear to me where to place the code and what exactly to put in.
Could someone explain to me how to do this in the simplest way possible?
I'm working through an R script.
Where df is the name of the data.frame, and the 0 in the round function is number of decimals:
df <- data.frame(lapply(df, function(x){ if(is.numeric(x)){round(x, 0)}else{x}}))
Data used:
df <- data.frame(x1 = c(84.2,105.2,79.2,140.2,108.2,79.2,112.2,118.2,114.2,92.2),
x2 = c(138.3,110.3,84.3,45.3,128.3,99.3,100.3,124.3,121.3,115.3),
x3 = as.character(c(138.3,110.3,84.3,45.3,128.3,99.3,100.3,124.3,121.3,115.3)),
stringsAsFactors = FALSE)
Nevermind, found a simple function round_half_up() which solved the problem!

Extract hour from a column of time [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
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"

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