Newbie to the R world needing assistance with column creation

I just can not get my head around this issue I need to solve.
I need to create a column called"ride_length" which will contain the duration of rides. I need to subtract the "started_at" column from the "ended_at" column and have the results formatted in minutes. please find attached a screenshot of the graph and code that I have already implemented into R. I have tried the "mutate" function but I am not sure what else to add. Please help!

You are more or less on the right track but it would be easier to answer your question if we had some data. A handy way to supply some sample data is the dput() function. In the case of a large dataset like yours something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your the name of your dataset . Copy the output and paste it here.

1 Like

Maybe if you try something like the following:

cyclyistic_q1$ride_length <- difftime(cyclyistic_q1$ended_at,cyclyistic_q1$started_at,units = 'mins')

I am afraid that is the best I can suggest without seeing your data as jkrideau has suggested.

I hope this helps

1 Like

Will do, can you see the picture that I have uploaded. You will be able to see the data on it. Nevertheless i will input the function that you suggest, one moment

I can see it but what you see is often not what you get. For example, the columns ended_at' and started_at` look like the are character variales but they could be actual date-time format and we would need to handle them very differently.

At a guess, if they are character then if we call your data dat1 one or both of these will work but it is much better to have real data,

# BASE R.
dat1$ride  <-  difftime(dat1$ended_at, dat1$started_at,  units = "mins")
# BASE R.
dat1$ride  <-  difftime(dat1$ended_at, dat1$started_at,  units = "mins")

# Tidyverse. 
mutate(dat1, ride = difftime(ended_at, started_at,  units = "mins")) 

My Base R code just another way of doing what @Rolando suggests.

1 Like

This code was successful, thank you a tonne. I literally spent the whole day trying to solve this haha

You are very welcome. It would be great if you could mark my suggestions as solved!

Done! thanks again
Hopefully I can get to a point were understanding R becomes second nature

You will but it takes a while. It's a weird language. I tend to think of it as beginner hostile.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.