0

I have a data frame that contains wifi download bandwidth and GPS data (latitude and longitude) on a transportation system. I want to determine from the data what the average bandwidth is when the vehicle is moving north, and what it is when it is moving south.

(bandwidth and latitude values from df)

bandwidth <- df$bandwidth

latitude <-df$latitude

(These both have 2800 entries)

(create empty vectors to fill with bandwidth values depending on whether the vehicle is moving north or south)

movingnorth <- vector('numeric')

movingsouth <- vector('numeric')

(If the train is moving north, fill the moving north vector with data from bandwidth vector)

for(y in latitude){

  if(latitude[y]>= latitude[y+1]){
  movingnorth <- c(movingnorth, received[y])}
  }

Here, I am basically saying if the latitude value is going up, then the vehicle is moving north, and therefore enter the bandwidth value from that location into the movingnorth vector. I would expect only a portion of the values from bandwidth vector to be added to the movingnorth vector, but all 2800 values are added. What am I doing wrong here?

5
  • You probably want to use for(y in seq_along(latitude)) or for(y in 1:length(latitude)). Commented Aug 15, 2017 at 18:56
  • This worked, thank you very much. Why does my original method not work? Commented Aug 15, 2017 at 19:01
  • don't you mean it'll be heading south if the current location is higher than the next? Commented Aug 15, 2017 at 19:03
  • also, latitude is a vector and you didn't specify exactly what you wanted to measure with it, so that's why using 1:length(latitude) makes sense Commented Aug 15, 2017 at 19:04
  • @sweetmusicality yup you are correct Commented Aug 15, 2017 at 19:24

2 Answers 2

1

Take advantage of R's vectorized operations. First we use diff to find the change between successive elements of latitude

latitude_change <- diff(df$latitude)

Now we have a vector whose length is 1 less than the length of latitude. Direction happens between the measurements, so that makes sense. Let's say we won't determine direction for the first measurement. So that means if latitude_change[i] > 0, then the train's northbound at time i - 1.

df$movingnorth <- c(FALSE, latitude_change > 0)

I'm keeping this part of df because it's related information, so a table's the perfect place for it.

Sign up to request clarification or add additional context in comments.

2 Comments

So by adding a column to the data frame, this command is simply entering all latitude change entries that are greater than 0, and then filling the remaining entries with FALSE?
The result's the same, but not exactly. Read up on vector arithmetic in the documentation: cran.r-project.org/doc/manuals/r-release/…
0

As lmo said, you want to use seq_along(latitude) or 1:length(latitude), which return the index instead of the actual element in latitude.

Also, you may want to double check that latitude[y+1] is correct. The current syntax assumes that the order of the latitude values in the data goes from the latest to the oldest. It is not possible to know if this is correct from the information you provide, but it may be the reverse.

As pointed out by Frank, you are growing your vector in a loop and that is bad practice (since it does not scale well and becomes very slow for large objects). Nathan Werth's answer suggests a vectorized implementation.

3 Comments

Besides logical errors, the OP is also growing something in a loop, bad practice in R; see chapter 2 of burns-stat.com/documents/books/the-r-inferno . Pointing to MCVE is nice, but I don't think it belongs in an answer. Better as a comment. Since you cannot comment under the OP, I'd say put it as a comment under your own answer.
I think incremental improvements to the suggested code are useful to the submitter, @Frank. That being said, I agree that growing the vector in a loop is bad practice (that becomes abundantly clear when working with large vectors) and a better answer would include improved code. Since a vectorized alternative has since been posted I am editing my answer to suggest that one. I am also moving the MCVE suggestion to a comment.
Including enough code to reproduce the problem would be helpful. See the guidelines for creating a Minimal, Complete, and Verifiable example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.