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"))
Related
I am trying to convert the values in column date (the data below) into date format yyyy-mm-dd:
I did it by using as.Date() and then change the output list into a dataframe as follows:
date_new = as.Date(df$Date, origin = '1899-12-30')
better_date = data.frame(Date = unlist(date_new))
I continue to use the converted data to filter June in each year and some other tasks as follows:
me_ff = df |>
filter(month(better_date) == 6) |>
mutate(sorting_date = better_date %m+% months(1)) |>
select(ticker,sorting_date,me_ff = Mkt_cap)
and the error message is:
"Error in `filter()`:
! Problem while computing `..1 = month(better_date) == 6`.
Caused by error in `as.POSIXlt.default()`:
! do not know how to convert 'x' to class “POSIXlt”
Run `rlang::last_error()` to see where the error occurred."
Could you please help me to solve the problem?
Thank you so much for your help!
Looks to me as if you confuse the data.frame object (the "table") with the date column. Untested code:
df <- transform(df, Date=as.Date(Date, origin = '1899-12-30')) |>
subset(strftime(Date, "%M")=="06")
should select the June rows.
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.
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 got the error when I use dplyr and lubridate package.
library(lubridate)
library(dplyr)
summary(data)
data %>%
group_by(yearmonth = format(ymd_hm(Time), "%Y-%m")) %>%
summarise(Ratio = mean(Emotion == "Positive"))
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