0

I have two shapefiles/dataframes (I can easily convert between types). One contains coordinates that form a boundary line and one contains coordinates for a set of points representing houses. They both have columns containing long and lat with matching column names between the dataframes.

What I would like to do is create a third dataframe that contains a subset of the house points that exist on the left of the boundary, but not the right.

Using if statements to test if a given house point is to the left of the dataframe, I have tried two versions:

df3 <- if(df1$lat < df2$lat){merge(df1,df2)}

df3 <- if(df1$lat[[i]] < df2$lat[[i]){merge(df1,df2)}

The first returns 0 observations and the second doesn't run, yielding Error: object 'i' not found. Suggestions?

edit: here is the first six rows of both dataframes. One of them has 130 rows, and the other has 3000 rows (the house points).

        lat     long                       geometry transaction.id
1 -80.12898 25.79037 LINESTRING (-80.12165 25.98...         145045
2 -80.13958 25.94337 LINESTRING (-80.12165 25.98...         240471
3 -80.19615 25.89209 LINESTRING (-80.12165 25.98...         495368
4 -80.12024 25.83410 LINESTRING (-80.12165 25.98...         129341
5 -80.14139 25.78359 LINESTRING (-80.12165 25.98...         146586
6 -80.12744 25.96239 LINESTRING (-80.12165 25.98...         232505
        lat     long                       geometry
1 -80.12165 25.98020 LINESTRING (-80.12165 25.98...
2 -80.11889 25.97520 LINESTRING (-80.11889 25.97...
3 -80.11876 25.97795 LINESTRING (-80.11876 25.97...
4 -80.11903 25.97236 LINESTRING (-80.11889 25.97...
5 -80.11918 25.96933 LINESTRING (-80.11903 25.97...
6 -80.11930 25.96639 LINESTRING (-80.11918 25.96...

edit:

v3 <- ifelse(df1$lat < df3$lat, 1, 0) 
df3 <- cbind(df1$lat, df1$long, df1$id, v3)

seems to do a clean job! thanks @r2evans but the values are not coming out right. I think it's because the lat and long column in the second dateframe listed above were coerced from an geometric column with two lats and two longs listed using st_geometry

Julia
  • 31
  • 3
  • 1
    I see a few things wrong initially: (1) `if` is a single-test, meaning its test must return a `logical` of length 1 (not 0, not 2, 3 is right out), consider `ifelse` if you need a vectorized if/then/else. (2) this assumes that `df1` and `df2` always have the same number of rows, which I find perhaps given your description (I could be wrong). (3) "to the left" sounds specific to a direction of travel ... do you mean "to the east/west" instead? – r2evans Jul 10 '19 at 22:39
  • 1
    Can you provide some sample data so we can help you better? If your dataframes are small/simple, you could try including the output of `dput(df1)` (and the same for `df2`). – Stewart Macdonald Jul 10 '19 at 23:10
  • @r2evans, yes, to the left/right of the boundary means in this case the same as east/west. I will try ifelse. – Julia Jul 11 '19 at 14:48
  • @StewartMacdonald I will update my original post with df1 and df2 – Julia Jul 11 '19 at 15:02
  • Both those dataframes appear to contain linestrings. I'm not sure where your points are. Hopefully this answer will help you: https://gis.stackexchange.com/questions/267857/selecting-features-above-or-below-a-line-using-r – Stewart Macdonald Jul 14 '19 at 10:50

0 Answers0