How to convert Numeric variable into date (yyyy/mm/dd) - R Studio - type-conversion

I will start off by saying I'm very new to R and I know I'm close just not sure what I'm missing.
I have a data frame called "sd" that has 1 variable "StatusDate"
> class(sd)
[1] "data.frame"
> str(sd)
'data.frame': 1572 obs. of 1 variable: $ StatusDate: num 20190309 20180328 20210123 20201125 20210219 ...
I am trying to convert the values to a date datatype in the format of "yyyy/mm/dd". I've tried using lubridate's ymd(), as.date(), as.date.character(), and as.date.numeric()
This seems to be the underlying error I receive when trying to create a new data frame with the updated date format
> new_status <- sd %>% ymd(StatusDate)
Error in lapply(list(...), .num_to_date) : object 'StatusDate' not found
Pretty much in everything I've ran it tells me object 'StatusDate' not found.
> sd %>% exists(StatusDate)
Error in exists(., StatusDate) : object 'StatusDate' not found
Any help on this error is greatly appreciated.

Related

I am trying to write an R script to calculate average ride length for each day based on user type using the aggregate function

I am trying to write an R script to calculate average ride length for each day and group by user type. The data structure has:
day_of_week (chr), Ride_length (num), user_type(chr)
The script is the following:
aggregate(divvy_2022$ride_length ~ divvy_2022$user_type ~ divvy_2022$day_of_week, FUN = mean)
I get this error:
Error in model.frame.default(formula = divvy_2022$ride_length ~ divvy_2022$user_type ~ :
object is not a matrix
What could be wrong?
I can't understand why the error comes up

Reading format "07/11/2022 09:22" as date in R (%d/%m/%y %H:%M:%OS)

I have a CSV file with dates on the format "07/11/2022 09:21:00" - which is %d/%m/%y %H:%M:%OS. I want to read them as dates but cannot make it work.
My data:
closed$closed_at: field where the dates are stored.
I tried the following:
<- as.Date(as.character("closed$closed_at"), format = "%d/%m/%y")
<- as.Date(closed$closed_at, format="%d%m%Y")
<- as.POSIXct(closed$closed_at, format="%d-%m-%Y %H:%M:%OS") - although I am aware that it does not make sense.
All of the above brings back the result: "NA"

tbl_svysummary choking after updating R to 4.2.1

I had a script (Markdown) that had been running without a problem, I updated R and R studio on my M1 Mac and now I get an error on tbl_svysummary.
I have setup my survey data. And it used to work.
Here is the code
#The data frame is labelled for having the questions on tables, this was working well. An example of the labelling:
label(srvydat$q3) <- "Q3. What is the postcode and suburb/place that you live in?"
#with each variable having a label
##the data setting it as a survey data
srvydatdes <- svydesign(id=~1, data= srvydat, weights=~thew)
## on only one variable summary it works still
cuales <- srvydat %>% select(starts_with("q4")) %>% names()
srvydatdes %>% tbl_svysummary(include=all_of(cuales), statistic=list(all_categorical()~"{p}%" ))
#no problem with above, but when I do a "By " table
cuales <- srvydat %>% select(starts_with(c("q9","q5a"))) %>% names()
chana <- srvydatdes %>% tbl_svysummary(include=all_of(cuales), by=q5a,statistic=list(all_categorical()~"{p}%" ))
#here I get the following error:
Error in mutate():
! Problem while computing df_stats = pmap(...).
Caused by error in left_join():
! Can't join on x$by x y$by because of incompatible types.
?ℹ x$by is of type >.
ℹ y$by is of type <factor<38ed8>>>.
Backtrace:
... %>% ...
dplyr:::left_join.data.frame(., svy_p, by = c("by", "variable_levels"))
I have checked that the variable being used is a factor.
And it used to work with previous versions.
Thanks
UPDATE
If i do the survey setting of data without the labelling, then tbl_svysummary works with the by (of course i lose the labels and I would still want to be able to have them)

Trying to build Expression for Table field to sort text dates, some with missing elements

Hi I am a newbie and have a problem I have been trying to solve for weeks. I have a table imported from excel with dates in text format (because dates go back to 1700s) Most are in the format "mmmyyyy", so it is relatively easy to add "1" to the date, convert to date format, and sort in correct date order. The problem I have is that some of the dates in the table are simply "yyyy", and some are empty. I cannot find an expression that works to convert these last two to eg 1 Jan yyyy and 1 Jan 1000 within the same expression. Is this possible, or would I need to do this in two queries? Sorry if this question is very basic - I cannot find an answer anywhere.
TIA
You can do something like:
Public Function ConvertDate(Byval Expression As Variant) As Date
Dim Result As Date
If IsNull(Expression) Then
Result = DateSerial(1000, 1, 1)
ElseIf Len(Expression) = 4 Then
Result = DateSerial(Expression, 1, 1)
Else
Result = DateValue(Right(Expression, 4) & "/" & Left(Expression, 3) & "/1")
End If
ConvertDate = Result
End Function

Why won't my factor column value change to a date value?

I know this is elementary but I can't seem to figure it out, even after reading other posts.
In a dataset, I want to convert an entire column into a date. The current class is factor.
The value in the field looks like this 12/25/2012
This is what I've tried.
C$DateofDeath=as.Date(C$DateofDeath,'%m/%d/%Y')
Error in as.Date.default(C$DateofDeath, "%m/%d/%Y") :
do not know how to convert 'C$DateofDeath' to class “Date”
C$DateofDeath=as.Date(C$DateofDeath,"%m/%d/%Y")
Error in as.Date.default(C$DateofDeath, "%m/%d/%Y") :
do not know how to convert 'C$DateofDeath' to class “Date”
Claims$DateofDeath=strptime(as.character(Claims$DateofDeath),format= '%m/%d/%Y')
Error in `$<-.data.frame`(`*tmp*`, "DateofDeath", value = list(sec = numeric(0), :
replacement has 0 rows, data has 71616
Claims$DateofDeath=strptime(as.character(Claims$DateofDeath),format= "%m/%d/%Y")
Error in `$<-.data.frame`(`*tmp*`, "DateofDeath", value = list(sec = numeric(0), :
replacement has 0 rows, data has 71616
Use as.POSIXct
C$DateOfDeath<-as.POSIXct(as.character(C$DateOfDeath), format = "%d/%m/%Y")
There are lots of R experts here but you have to specify R as one of your tags to get them to notice your question.
Looks like you have tried a bunch of combinations but not the right one.
> C <- data.frame(DateofDeath="12/25/2012",other=TRUE)
> as.Date(as.character(C$DateofDeath),format="%m/%d/%Y")
[1] "2012-12-25"
Notice that as.Date() takes a character input, not a factor. So you need to convert to character, then to Date.
Your strptime() versions seem fine to me except that you call are referring to the dataframe Claims instead of C. Actually strptime() should convert the factor to character for you, so you don't need the as.character() part with those.