-1

I have dataframe coords_pa with two columns(x=longitude, y = latitude). For example:

coords_pa <-data.frame(x=c(35.75004,41.41671,67.41672,49.58338),y=c(63.58333,63.41666,38.75004, 54.58338))

How could I create new dataframe coords_pa_new with two columns (x,y), but it will contain only coordinates with condition: x>40 and y>64?

I tried to find and succeed this task, but I do not know how to convert it in one dataframe:

kf = ifelse(coords_pa$y>64,1,0)

newdata = cbind(kf,coords_pa)

ff = ifelse(coords_pa$x>40,1,0)

newdata1 = cbind(ff,coords_pa)

huanpops
  • 53
  • 4

2 Answers2

0

You can subset coords_pa using [ by combining both conditions with &:

coords_pa[coords_pa$x > 40 & coords_pa$y > 64,]
GKi
  • 37,245
  • 2
  • 26
  • 48
0

In base R, you can subset, like so:

coords_pa[coords_pa$x > 40 | coords_pa$y > 64,]

The | operator specifies that x > 40 OR y > 64. If you rather mean that both conditions must be satisfied, change the operator to & (but there's no result in your sample)

Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34