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.
Related
I'm extracting excel file to have multiple values in character including dates. From excel I take date_1. date_2 i get it from postgresql using dbgetquery which is always data.frame.
date_1 = 2017-05-22
class(date_1) # this is "character"
date_2 = "2017-05-20"
class(date_2) # this is "data.frame"
days <- difftime(as.Date(date_1), as.Date(date_2), units = "days")
print(days)
calculate_age = julian(as.Date(date_1), as.Date(date_2))
print(calculate_age)
I'm getting below 2 error.
1. "Error in as.Date.default(date_2) :
do not know how to convert 'date_2' to class “Date”"
2. Error in UseMethod("julian") :
no applicable method for 'julian' applied to an object of class "character"
Anyone please suggest. I tried many. but no help. Also I'm new to R and Postgresql.
You have a good foundation, but your as.Date has not specified what date format you are supplying. By specifying that the date strings are 'yyyy-mm-dd', days is calculated using the difftime( ) function.
date_1 = "2017-05-22"
date_2 = "2017-05-20"
days <- difftime(as.Date(date_1,format="%Y-%m-%d"),
as.Date(date_2,format="%Y-%m-%d"),
units = "days")
print(days)
Time difference of 2 days
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 summing up some values on a data frame, which works fine, until i apply a restriction with dates. I have tried declaring my "z" column of dates as follows:
Table$z <- as.Date(Table$createtime, "%m/%d/%Y",tz = "GMT")
and my End date as:
EndDate <- as.Date('2017-02-01')
These then are used in:
malone <- tbl_df(Table)
malone1 <- malone%>%
group_by(InstallationDate)%>%
summarize(Amount = sum(USAmount), count = n(), filter(z < EndDate))
Unfortunately i am not able to make this work and i am getting the following error:
Error in charToDate(x) :
character string is not in a standard unambiguous format
I am trying to run code on a data frame up to a certain date. I have individual game statistics, the second column is Date in order. I thought this is how to do this however I get an error:
Error in `[.data.frame`(dfmess, dfmess$Date <= Standingdate) :
undefined columns selected
Here is my code:
read.csv("http://www.football-data.co.uk/mmz4281/1516/E0.csv")
dfmess <- read.csv("http://www.football-data.co.uk/mmz4281/1516/E0.csv", stringsAsFactors = FALSE)
Standingdate <- as.Date("09/14/15", format = "%m/%d/%y")
dfmess[dfmess$Date <= Standingdate] -> dfmess
You probably want to convert dfmess$Date to as.Date first prior to comparing. In addition, per #Roland's comment, you require an additional comma ,:
dfmess <- read.csv("http://www.football-data.co.uk/mmz4281/1516/E0.csv", stringsAsFactors = FALSE)
dfmess$Date <- as.Date(dfmess$Date, "%m/%d/%y")
Standingdate <- as.Date("09/14/15", format = "%m/%d/%y")
dfmess[dfmess$Date <= Standingdate, ]