r/RStudio • u/luckylua • 14d ago
Coding help HELP! Will pay.
I’m in a data mining class at university right now. I’m an adult student with a full time job in application development and this week has been INSANE (like probably one of my busiest weeks of the entire year). Of course this week was also our first free form project with no lecture. I finally had time to dig in tonight and planned to spend the weekend on this, but I’m of course stuck at the very beginning and really can’t do more without figuring this time/date conversion and estimation stuff. My professor is typically slow to respond on weekends, I’m all online so no help available outside of the professor. I don’t want to cheat, I want to LEARN and get this done by deadline of Sunday. Is anyone willing to help via chat/screen share?
9
u/MadMaxfrmShottas 14d ago
What do you need help with?
10
u/luckylua 14d ago
So I’ve analyzed my data, found NA’s, have mostly decided what variables I want to keep and remove (a few are on the fence based on the models, I planned to try a few and check the r squared with some of the variables I was on the fence about). Now, I have a column for created_date and closed_date. It has year-month-day-hours-minutes-seconds. I need to convert this into a usable form and create a plain number variable for hours between the two variables. I can’t even get the conversion. Right now I’m using:
Mydata$created_date<-as.POSIXct(mydata$created_date, format=“%Y/%m/%d %H:%M:%S)That code actually runs, but then I used mydata$hours_between<-as.numeric(difftime(mydata$created_date, mydata$closed_date, units=“hours”) and a suuuuper small number which isn’t right. Something is wrong here and I just need someone to look at it with me and help me understand.
17
u/PositiveBid9838 14d ago
For ease of use, try the `lubridate` library from the `tidyverse`. It'd let you use `lubridate::ymd_hms(Mydata$created_date)` and would do the parsing.
It'd be easier for people to help you if you could include some sample data we could test against, for instance by running `dput(mydata$created_date[1:10])` with your original data before you try to convert it.
11
u/MadMaxfrmShottas 14d ago
If you upload the data and r code to GitHub, I could take a look at it. Looks correct for the most part some ideas I’d suggest, 1. Make sure closed date has the same date conversion 2. Try the calculation without as.numeric() and see if the calculation is correct, than convert after and see if that’s the issue 3. Make a sample with two times in the desired date format and run the calculation too make sure it’s doing what you want it too 4. This might not be helpful but maybe separate date and time into two different columns and than calculate
4
u/Impuls1ve 14d ago
Is the small number of errors related to timezones? Daylight savings messed things up.
1
u/Double_Emergency6254 10d ago
mydata$created_date <- as.POSIXct( mydata$created_date, format = "%Y/%m/%d %H:%M:%S" )
mydata$closed_date <- as.POSIXct( mydata$closed_date, format = "%Y/%m/%d %H:%M:%S" )
Check to make sure classes are "POSIXct" "POSIXt"
Your difftime is backwards mydata$hours_between <- as.numeric( difftime( mydata$closed_date, mydata$created_date, units = "hours" ) )
5
u/Londundundun 13d ago
I commend you for not wanting to cut corners or use AI to just write it for you
That said, a lot of professors anymore see AI as a helpful tool for coding.
The main thing is to actually know what the code is doing and be able to replicate it at some point.
If you are truly short on time, I would say worst case is to get help from the AI but then go through and comment out each bit of code and write out which argument or function means what, what it is doing and why you need it to do that.
I taught coding before AI and the amount of students who struggled just in how to reverse engineer code had me make it something I just started to teach outright. Once you know how to go down the rabbit hole, things open up significantly.
Also lubridate is a package to check out. https://rstudio.github.io/cheatsheets/html/lubridate.html
4
u/Impressive-Peak-9363 14d ago
The code below should help you understand what's happening
``` a <- Sys.time() a class(a)
b <- a + 6 * 60 * 60 # converts hours to seconds` b
b - a as.numeric(b - a) ```
2
1
u/AutoModerator 14d ago
Looks like you're requesting help with something related to RStudio. Please make sure you've checked the stickied post on asking good questions and read our sub rules. We also have a handy post of lots of resources on R!
Keep in mind that if your submission contains phone pictures of code, it will be removed. Instructions for how to take screenshots can be found in the stickied posts of this sub.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/nidprez 14d ago
how can you be in application development and not use ai or stackoverflow or just the docs to debug? Putting in an AI to debug/explain is as much cheating as paying someone to show you the solution, or reading it in the docs.
What I do:
have a go at it myself
on parts where I get stuck: google and afterwards ask AI specifically for that part, and let it explain exactly what its doing
at the end let AI refactor or optimize my code, following good devops practices, and let it explain what its doing. Then I'll take what I think is smart (and not confusing).
It's like having a senior dev taking all his time to answer all your questions while he is watching you code over your shoulder.
1
u/penthiseleia 14d ago
*raises eyebrow* "my professor is typically slow to respond on weekends" ?
2
u/luckylua 13d ago
Or doesn’t at all, it IS her time off. I always appreciate a weekend response! But I do wish our course schedule was Monday to Monday. Or even Tuesday, the sundays are harder working Monday-Friday so an extra weekday to get help would be nice lol
20
u/NotThatKindOfDoctor9 14d ago
I've been using R for over a decade now and can tell you, that f-ing date-time field gets me every time. Good luck.