Need help mutating columns

Hi everyone.

I need help creating a new column from existing ones.

I want to create ride_distance column by calculating the difference in these 4 columns - start_lat, start_lng, end_lat, and end_lng columns.

Note: some columns values are negative but the data type is numeric

Hi, welcome to the forum.

At the very least, we are going to need some sample data. It looks like you are using latitude and longitude data so we ned to know its format and so on.

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need.

More generally we may need a FAQ: How to do a minimal reproducible example ( reprex ) for beginners

Hi, here is a screenshot of the dataframe


I am attempting to recreate this code

Can you paste a small part of your data frame using dput(head(yourdataframe, 10))?

Hello,
I'm sure you shared this image with the best intentions, but perhaps you didnt realise what it implies.
If someone wished to use example data to test code against, they would type it out from your screenshot...

This is very unlikely to happen, and so it reduces the likelihood you will receive the help you desire.
Therefore please see this guide on how to reprex data. Key to this is use of either datapasta, or dput() to share your data as code

start_lat <- c(41.9, 42, 42, 41.9, 41.9)
start_lng <- c(-87.7, -87.7, -87.7, -87.8, -87.6)
end_lat <- c(42, 41.9, 42, 41.9, 41.9)
end_lng <- c(-87.7, -87.7, -87.7, -87.8, -87.6)


distance <- data.frame(start_lat, end_lat, start_lng, end_lng)

Created on 2022-10-15 with reprex v2.0.2

I have not checked tthe math but is this about what you want?

library(geosphere) # Distance in metres

start_lat <- c(41.9, 42, 42, 41.9, 41.9)
start_lng <- c(-87.7, -87.7, -87.7, -87.8, -87.6)
end_lat <- c(42, 41.9, 42, 41.9, 41.9)
end_lng <- c(-87.7, -87.7, -87.7, -87.8, -87.6)


distHaversine(matrix(c(start_lng, start_lat), ncol = 2),
              matrix(c(end_lng,  end_lat), ncol = 2))

Yes! Thank you so very much!

This topic was automatically closed 42 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.