PigWolf - 1 year ago 133

R Question

I would like to apply the destPoint function over my dataframe but I am struggling to get it to work.

I have the following code working correctly so I am falling down when applying it over my dataframe:

`sectorCoor <- as.vector(t(destPoint(c(-8.609117,52.69373),c(360,9.6,19.8,30,40.2,50.4,60.6),2500)))`

The transpose and as vector as purely cosmetic so that I have a single line of values for each row of my dataframe.

where -8.609117,52.69373 are the origin lat longs (col H&I in my sample file),

360,9.6,19.8,30,40.2,50.4,60.6 (col AQ-AW) are the bearings I wish to calculate the endpoint for and 2500 is the distance I wish to calculate over.

When I try to incorporate the apply function using the code below I get errors:

`siteCoor <- apply(RNC20[,c(7:8,42:48)],1, function(x) destPoint(c(x[1]:x[2]),c(x[3]:x[9]),2500))`

Error in .pointsToMatrix(p) : Wrong length for a vector, should be 2

I believe the p refers to the p variable in destPoint which is the long lat input which can be a vector of two numbers or a matrix of two columns. In my code above I have referenced these two inputs as a vector

`c(x[1]:x[2])`

I have some sample data here if it helps

As a secondary problem how can I change the distance element (2500 at the end) in the equation to be based on the contents of column J? If J = 10564 set the distance to 2500, if J = 10589 distance = 2200, if J = 10613 set distance = 1900 and if J = 3088 set the distance = 3000?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Not `x[1]:x[2]`

but `x[1:2]`

. And it is vectorized, so you needn't use `c()`

.

```
apply(RNC20[,c(7:8,42:48)],1, function(x) destPoint(x[1:2], x[3:9], 2500))
```

I can't understand how to get distance values from J. Are J some kind of ID? If so, I think it is a steady method to add distance `column`

. If not (or you want to solve it using function), it woud be good idea to make `function()`

that take J as an argument and output distance.

for example:

```
JtoDist <- function(x) x^2
apply(RNC20[,c(7:8,42:48, JcolNum)] function(x) destpoint(x[1:2], x[3:9], JtoDist(x[10])))
```

**[EDITED]**

```
library(geosphere)
RNC20 <- read.csv("RNC.csv")[,-1]
# Maybe this is what you want ?
t(apply(RNC20[,c(7:8,42:48)],1, function(x) t(destPoint(x[1:2], x[3:9], 2500))))
# or here ? (long format)
library(reshape2)
res <- apply(RNC20[,c(7:8,42:48)],1, function(x) destPoint(x[1:2], x[3:9], 2500))
colnames(res) <- RNC20[,"Cell.Reference"]
rownames(res) <- rep(colnames(RNC20[,42:48]), 2)
melt.res <- cbind(melt(res[1:7,], value.name = "Longitude"), melt(res[8:14,], value.name = "Latitude"))[, c(2,1,3,6)]
head(melt.res)
# question2: an functional approach
J <- c(10564,10589, 10613, 3088)
set.seed(1); RNC20_J <- cbind(RNC20, J = sample(J, 18, replace = T))
JtoDist <- function(x) switch( which(c(x==10564, x==10589, x== 0613, x==3088)), 2500, 2200, 1900, 3000)
JtoDist(10589) # [1] 2200
t(apply(RNC20_J[,c(7:8,42:49)], 1, function(x) t(destPoint(x[1:2], x[3:9], JtoDist(x[10])))))
```