0

I have a dataframe (df) with a column of Latitude (Lat), and I need to match up the corresponding Longitude value (based off relationships in another dataset). New column name is 'Long_matched'.

Here, I am trying to write a new value in the column 'Long_matched' at the corresponding row to latitudes between -33.9238 and -33.9236. The data in 'Lat' has many more decimal places (e.g: -33.9238026666667, -33.9236026666667, etc.). As I will be applying this code to multiple datasets over the same geographical location (hence the long decimals will vary slightly), I want to write Longitude values which fall within a a 0.0002 degree range.

Some attempts of code I have tried include:

df$Long_matched <- ifelse(df$Lat< -33.9236 & df$Lat> -33.9238, 151.2279 , "N/A")

or

df$Long_matched[df$Lat< -33.9236 & df$Lat> -33.9238] <- 151.2279

I think I need to use a for loop to loop through the rows and an if statement, but struggling to figure this out - any help would be appreciated!

Resulting output should look something like this:

Lat                   Long_matched
-33.9238026666667     151.2279
-33.9236026666667     (new long value will go here)
9
  • Are you trying to do the comparison between adjacent elements of 'Lat' ? Commented Jun 7, 2018 at 2:38
  • Post a few rows of example input. Your code is confusing because your first line modifies df and your second line modifies a different data frame named df_avg... probably you just need to merge(df1, df2)? But very hard to tell without sample inputs and desired output. Commented Jun 7, 2018 at 2:39
  • No, I just want to add a new value in the 'Long_matched' column in the row that matches particular 'Lat' values. I already know what the value I want to add is. Commented Jun 7, 2018 at 2:39
  • 1
    Can you round Lat to a number to be directly matched? Commented Jun 7, 2018 at 2:51
  • 1
    If you use "N/A" the entire column will be coerced to class character. Commented Jun 7, 2018 at 4:18

1 Answer 1

3

Everything said in the comments applies, but here is a trick you can try: In the following code, you will need to replace text with numbers.

Latitude_breaks <- seq(min_latitude, max_latitude, 0.0002) # you need to replace `min_latitude`, `max_latitude`, and `increment` with numbers
Longitude_values <- seq(first, last, increment) # you need to replace `first`, `last` and `increment` with numbers
df <- within(df, {
  # make a categorical version of `Lat`
  Lat_cat <- cut(Lat, Latitude_breaks)
  Long_matched <- Longitude_values[Lat_cat]
})

A few notes:

  1. the values between min_latitude and min_latitude + 1 will be assigned to the values of Longitude marked first.
  2. The length of Latitude_beaks should be one more than the length of Longitude_values.
  3. Values of Lat outside of Latitude_breaks will becomes NAs.

This works by exploiting a nice feature of factors - they are stored as integers. So we can use them to index another vector - in this case, Longitude_values.

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

Comments

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.