Reputation: 476
I need to do some computations with the data stored in a dataframe. ANd put the result into new column of this dataframe.
Initial dataframe:
> str(mydf)
'data.frame': 1122 obs. of 6 variables:
$ MMSI : num 2.73e+08 2.73e+08 2.73e+08 2.73e+08 2.73e+08 ...
$ MMSI.1 : num 2.73e+08 2.72e+08 2.72e+08 2.72e+08 6.67e+08 ...
$ LATITUDE : num 46.9 46.9 46.9 46.9 46.9 ...
$ LONGITUDE : num 32 32 32 32 32 ...
$ LATITUDE.1 : num 46.9 46.9 46.9 46.9 46.9 ...
$ LONGITUDE.1: num 32 32 32 32 32 ...
Now I need to add a new column which contain the result of operation with the data of the current raw..
running next code:
library(geosphere)
> mydf$distance <- with(mydf, distGeo(c(mydf$LONGITUDE,mydf$LATITUDE),c(mydf$LONGITUDE,mydf$LATITUDE)))
Error in .pointsToMatrix(p1) : Wrong length for a vector, should be 2
I understand that's the structure of the data for function distGeo should be different. How to fix this error or how to change the code to get the distances between point in a new column?
Upvotes: 0
Views: 1595
Reputation: 4357
Without having data to look at, it looks like you are trying to calculate the distance between a single point. The second point should likely include .1
at the end of the column name
library(geosphere)
mydf$distance <- with(mydf, distGeo(c(LONGITUDE, LATITUDE), c(LONGITUDE.1, LATITUDE.1)))
update
It looks like the error is that you're passing the entire data frame instead of each row individually. Try this
apply(mydf, 1, function(x) distGeo(x[c("LONGITUDE","LATITUDE")],x[c("LONGITUDE.1","LATITUDE.1")]))
Or just pass specific data columns to function as it accepts a matrix
distGeo(mydf[,c("LONGITUDE", "LATITUDE")], mydf[,c("LONGITUDE.1", "LATITUDE.1")])
Upvotes: 1