In the output of the code below the variables day and sales are in the format that I need but not the type, it outputs type chr instead. The variables should be date and num respectively. I've tried many things but either I get chr or some sort of error. For instance, using as.Date() doesn´t change the variable day to the format "%d/%m/%Y". The code with sample data:
library(dplyr)
library(lubridate)
df <- data.frame(matrix(c("2017-09-04","2017-09-05",103,104,17356,18022),ncol = 3, nrow = 2))
colnames(df) <- c("DATE","ORDER_ID","SALES")
df$DATE <- as.Date(df$DATE, format = "%Y-%m-%d")
df$SALES <- as.numeric(as.character(df$SALES))
df$ORDER_ID <- as.numeric(as.character(df$ORDER_ID))
TOTALSALES <- df %>%
select(ORDER_ID,DATE,SALES) %>%
mutate(weekday = wday(DATE, label=TRUE)) %>%
mutate(DATE=as.Date(DATE)) %>%
filter(!wday(DATE) %in% c(1, 7) & !(DATE %in% as.Date(c('2017-01-02','2017-02-27','2017-02-28','2017-04-14'))) ) %>%
group_by(day=floor_date(DATE,"day")) %>%
summarise(sales=sum(SALES)) %>%
data.frame()
TOTALSALES$day <- TOTALSALES$day %>%
as.POSIXlt(, tz="America/Sao_Paulo") %>%
format("%d/%m/%Y")
TOTALSALES$sales <- TOTALSALES$sales %>%
format(digits=9, decimal.mark=",",nsmall=2,big.mark = ".")
TOTALSALES$day <- as.Date(df$DATE, format = "%d/%m/%Y")
Any idea how can I solve this problem or a direction on how it should be done ?
Appreciate any help
I'm not sure I understand your question.
To print a Date object in a particular date-time format you can use format
# This *converts* a character vector/factor to a vector of Dates
df$DATE <- as.Date(df$DATE, format = "%Y-%m-%d")
# This *prints* the Date vector as a character vector with format "%d/%m/%Y"
format(df$DATE, format = "%d/%m/%Y")
Minimal example
ss <- c("2017-09-04","2017-09-05")
date <- as.Date(ss, format = "%Y-%m-%d")
format(date, format = "%d/%m/%Y")
#[1] "04/09/2017" "05/09/2017"
Related
I am working with R, and have a column in datetime (2021-05-31 13:25:10) and I want to change it to time only (HH:MM:SS) by removing the date.
Thanks.
We can use as.ITime from data.table
library(data.table)
df1$time <- as.ITime(df1$datetime)
If we need to get the hours only
library(dplyr)
library(lubridate)
df1 <- df1 %>%
mutate(datetime = ymd_hms(datetime),
hours = hour(datetime), mins = minute(datetime),
secs = second(datetime))
data
df1 <- data.frame(datetime = "2021-05-31 13:25:10")
You can use format to extract time component from date-time.
df <- data.frame(datetime = Sys.time() + sample(10))
df$time <- format(df$time, '%T')
I have some different dataframes containing date and need to create and format the data.
Step 1. Convert dataframe$date as date. Step 2&3. Create new columns with week and year. Step 4&5 convert week and year to numeric.
Since I need to do this multiple times, I would like to create a function out of it.
dataframe$date <- as.Date(dataframe$date, "%Y-%m-%d")
dataframe$week <- strftime(dataframe$date, format = "%V")
dataframe$year <- strftime(dataframe$date, format = "%Y")
dataframe$week <- as.numeric(dataframe$week,as.numeric)
dataframe$year <- as.numeric(dataframe$year,as.numeric)
My attempt:
format.dataframe <- function(a) {
paste(a, $date, sep="") <- as.Date(paste(a, $date, sep=""), "%Y-%m-%d")
paste(a, $week, sep="") <- strftime(paste(a, $date, sep=""), format = "%V")
paste(a, $year, sep="") <- strftime(paste(a, $date, sep=""), format = "%Y")
}
So a should be the name of the dataframe I want to format.
You can use transform in base R :
format.dataframe <- function(a) {
transform(a, date = as.Date(date, "%Y-%m-%d"),
week = as.numeric(strftime(date, format = "%V")),
year = as.numeric(strftime(date, format = "%Y")))
}
a <- format.dataframe(a)
Or with mutate in dplyr :
library(dplyr)
format.dataframe <- function(a) {
a %>%
mutate(date = as.Date(date, "%Y-%m-%d"),
week = as.numeric(strftime(date, format = "%V")),
year = as.numeric(strftime(date, format = "%Y")))
}
a <- format.dataframe(a)
I want to convert the following string into year-month?
df <- tribble(
~date,
'20201227',
)
Here is the desired output.
new_df <- tribble(
~date,
'2020-12',
)
How can I do this?
Convert to Date class and use format
library(dplyr)
df <- df %>%
mutate(date = format(as.Date(date, '%Y%m%d'), '%Y-%m'))
Another possible option using gsub (but the as.Date answer by #akrun is more recommended)
transform(
df,
date = gsub("(\\d{4})(\\d{2}).*", "\\1-\\2", date)
)
gives
date
1 2020-12
I am having trouble standardizing the Date format to be dd-mm-YYYY, This is my current code
Dataset
date
1 23/07/2020
2 22-Jul-2020
Current Output
df$date<-as.Date(df$date)
df$date = format(df$date, "%d-%b-%Y")
date
1 20-Jul-0022
2 <NA>
Desired Output
date
1 23-Jul-2020
2 22-Jul-2020
You can try this way
library(lubridate)
df$date <- dmy(df$date)
df$date <- format(df$date, format = "%d-%b-%Y")
# date
# 1 23-Jul-2020
# 2 22-Jul-2020
Data
df <- read.table(text = "date
1 23/07/2020
2 22-Jul-2020", header = TRUE)
I've saved your example data set as a dataframe named df. I used group_by from dplyr to all each date to be converted separately to the correct format.
library(tidyverse)
df %>%
group_by(date) %>%
mutate(date = as.Date(date, tryFormats = c("%d-%b-%Y", "%d/%m/%Y"))) %>%
mutate(date = format(date, "%d-%b-%Y"))
I have the following dataframe
Date Time
10/03/2014 12.00.00
11/03/2014 13.00.00
12/03/2014 14.00.00
I want to create one single column as follows
DT
10/03/2014 12.00.00
11/03/2014 13.00.00
12/03/2014 14.00.00
when I run
data$DT <- as.POSIXct(paste(x$Date, x$Time), format="%d-%m-%Y %H:%M:%S")
I get a column DT with all NA values.
Data$DT <- as.POSIXct(as.character(paste(data$Date, data$Time)), format="%d/%m/%Y %H.%M.%S")
OR
data$Time <- gsub('\\.',':',data$Time)
data$Date <- gsub('/','-',data$Date)
data$DT <- as.POSIXct(as.character(paste(data$Date, data$Time)), format="%d-%m-%Y %H:%M:%S")
Use the package lubridate:
data$DT <- with(data, ymd(Date) + hms(Time))
If you want the column to be a POSIXct, do the following after that:
data$DT <- as.POSIXct(data$DT)
This should be a very common problem, hence contributing with a reproducible answer using dplyr:
## reproducible example
library(dplyr)
library(magrittr)
DF <- data.frame(Date = c("10/03/2014", "11/03/2014", "12/03/2014"),
Time = c("12.00.00", "13.00.00", "14.00.00"))
DF_DT <- DF %>%
mutate(DateTime = paste(Date, Time)) %>%
mutate(across('DateTime', ~ as.POSIXct(.x, format = "%d/%m/%Y %H.%M.%S")))