0

Im trying to iterate over a dataset to calculate a gcIntermediate distance between the root and the destination columns. I come from programming in python and c# so I understand loops, but not how to do them in R.

Routes <- list()

For (coord in routeCoords)
{
    if(!is.na(coord$Root[0])
        inter <- gcIntermediate(coord$Root, coord$Destination, n=5)
        list.append(routes, inter)
}

The error is

Error: $ operator is invalid for atomic vectors

And it's on the if statement

The dataframe is 2 columns (Root and Destination), each which a tuple with 2 items (lat and lon)

Example Data:

root    destination
(2,5)   (2,6)
(1,7)   (2,8)

I'm going to use the output of this function to plot it onto a ggplot that I already made to represent the routes that people can take from one node to another in physical space.

FallingInForward
  • 285
  • 2
  • 4
  • 12
  • your error means you cannot use `$` to access the elements of your vector. Instead try `coord[row,column]` If you want all rows then simply write `coord[,column]` instead – Daniel O Jul 07 '20 at 11:09
  • `R` is case sensitive, so `For` and `routes` are different to `for` and `Routes`. Nevertheless, please share a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Martin Gal Jul 07 '20 at 11:17
  • If you show us some example data and expected output, we might be able to find an option that does not involve `for`-loops. In general, there are often better approaches in R. – mhovd Jul 07 '20 at 11:24
  • I'd just like to apologise for my late responses. They're working on my broadband and I'm reliant on my data. I'll try to reformat the question – FallingInForward Jul 07 '20 at 11:42
  • @mhh I added in the sample data and expected output. What else do you need from me to help me to solve this? – FallingInForward Jul 07 '20 at 12:00

0 Answers0