I have a column in format 19990101 that I would like to change to date format. It is currently as integer.
I've tried the following but it results in
Error: unexpected symbol in: mutate(Date = Date, Date = as.integer(format)date.
mutate(Date = Date,
Date = as.integer(format)date, "%Y%m%d")
mutate(Date = Date,
Date = as.Date(format(x), "%Y%m%d")
The above code solved my issue.
Related
The date is currently yyyy-mm-dd, but I want it as mm/dd/yyyy.
I've tried various methods which either result in absolutely nothing happening or getting errors such as:
Warning message:
Problem while computing date = lubridate::mdy(date).
ℹ All formats failed to parse. No formats found.
Error in mutate():
! Problem while computing date = format(date, "%m/%d/%Y").
Caused by error in format.default():
! invalid 'trim' argument
Backtrace:
mass %>% mutate(date = format(date, "%m/%d/%Y"))
base::format.default(date, "%m/%d/%Y")
How do I fix this?
library(dplyr)
library(lubridate)
mass = read.csv('https://raw.githubusercontent.com/bandcar/Examples/main/mass_killing_incidents_public.csv')
mass2 = mass
mass2$date = format(mass$date, format = "%m/%d/%Y")
mass2 = mass %>%
mutate(date = lubridate::mdy(date),
date = strftime(date, "%m/%d/%Y"))
mass2 = mass %>%
mutate(date = format(date, '%m/%d/%Y'))
We could do
library(dplyr)
library(lubridate)
mass %>%
mutate(date = lubridate::ymd(date), date = strftime(date, "%m/%d/%Y"))
I have two databases where I need to combine columns based on 2 common Date columns, with condition that the DAY for those dates are the same.
"2020/01/01 20:30" MUST MATCH "2020/01//01 17:50"
All dates are in POSIXct format.
While I could use some pre-cprocessing with string parsing or the like, I wanted to handle it via lubridate/dplyr like:
DB_New <- left_join(DB_A,DB_B, by=c((date(Date1) = date(Date2)))
notice I am using the function "date" from dplyr to rightly match condition as explained above. I am though getting the error as below:
DB_with_rain <- left_join(DB_FEB_2019_join,Chuvas_BH, by=c(date(Saida_Real)= date(DateTime)))
Error: unexpected '=' in "DB_with_rain <- left_join(DB_FEB_2019_join,Chuvas_BH, by=c(date(Saida_Real)="
Within in the by, we cannot do the conversion - it expects the column name as a string. It should be done before the left_join
library(dplyr)
DF_FEB_2019_join %>%
mutate(Saida_Real = as.Date(Saida_Real, format = "%Y/%m/%d %H:%M")) %>%
left_join(Chuvas_BH %>%
mutate(DateTime = as.Date(DateTime, format = "%Y/%m/%d %H:%M")),
by = c(Saida_Real = "DateTime"))
With lubridate function, the as.Date can be replaced with ymd_hm and convert to Date class with as.Date
As part of an automation process, I have a df with a date variable. There are only two date levels today and yesterday.
I am looking to recode this date variable so that todays and yesterdays dates in %d-%m-%Y
format
df %>%
mutate(date2 = recode(date, "today" = Sys.Date(), "yesterday" = Sys.Date()-1))
This returns an error:
Error in UseMethod("recode") :
no applicable method for 'recode' applied to an object of class "Date"
I would be really grateful for any advice
I think this is what you want? Using a case_when to check if the date equals today(), and if so, classifying as 'today', otherwise classifying as 'yesterday'?
df %>%
mutate(date2 = case_when(
date==lubridate::today() ~ "Today",
TRUE ~ "Yesterday"))
I am setting up my variable to hold the filtered dates of when there are suspicious phone calls through the 2007 year. However, something is fishy with setting the Date format in the Date column.
I've tried without format, moving the %m/%d/%Y to %Y/%m/%d, I've tried switching from 01/01/2007 to 2007/01/01 as well as the 01-01-2007.
SFPD_reports <- read_csv("SFPD_Incident_Reports_2003_May2018.csv")
SetDate_SetCrime <- Create_Fil_Sus %>%
mutate(Date = format(as.Date(Date, "%m/%d/%Y")))
SetDate_SetCrime <- select(Create_Fil_Sus, IncidntNum, Descript, DayOfWeek, Date, Location)%>%
filter(Date >= (as.Date("01/01/2006", "%m/%d/%Y") & (Date <= as.Date("12/31/2007", "%m/%d/%Y"),
Descript == "SUSPICIOUS PERSON"))%>%
mutate(lat= (str_sub(Location, 26, str_length(Location))),
lon= (str_sub(Location, 9, 24)))
But I get:
Error in charToDate(x) :
character string is not in a standard unambiguous format
# SetDate_SetCrime <- Create_Fil_Sus %>%
# mutate(Date = format(as.Date(Date, "%m/%d/%Y")))
Create_Fil_Sus <- mutate(Create_Fil_Sus, Date = as.Date(Date, "%m/%d/%Y"))
Mutating the same object, produced the correct formate (the uncommented line of code worked). Yippie
I am trying to format a string column to a date-time serie.
The row in the column are like this example: "2019-02-27T19:08:29+000"
(dateTime is the column, the variable)
mutate(df,dateTime=as.Date(dateTime, format = "%Y-%m-%dT%H:%M:%S+0000"))
But the results is:
2019-02-27
What about the hours, minutes and seconds ?
I need it to apply a filter by date-time
Your code is almost correct. Just the extra 0 and the as.Date command were wrong:
library("dplyr")
df <- data.frame(dateTime = "2019-02-27T19:08:29+000",
stringsAsFactors = FALSE)
mutate(df, dateTime = as.POSIXct(dateTime, format = "%Y-%m-%dT%H:%M:%S+000"))