1

In python using pandas for slicing a column, where data is the dataset and 'pickup_latitude is a column following line will work

data = data[(data['pickup_latitude']<=90) & (data['pickup_latitude']>=-90)]

What will be the equivalent of the above in R and that also for multiple columns with different range of values ?

surya rahul
  • 833
  • 2
  • 14
  • 27

1 Answers1

0

The same thing will work, but you need to add a comma at the end to show that you're indexing rows:

data = data[(data['pickup_latitude']<=90) & (data['pickup_latitude']>=-90), ]

For example, on the built-in mtcars data:

> mt = mtcars[mtcars['mpg'] <= 20 & mtcars['mpg'] >= 15, ]
> mt
                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Merc 280          19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C         17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Merc 450SE        16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
Merc 450SL        17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
Merc 450SLC       15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
Dodge Challenger  15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
AMC Javelin       15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Pontiac Firebird  19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Ford Pantera L    15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
Ferrari Dino      19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Maserati Bora     15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8

A bit of explanation: in R, data frames are lists of columns. If you use square brackets with a single argument, the assumption is you are subsetting columns, as in mtcars['mpg'] is the mpg column of mtcars and data['pickup_latitude'] is the pickup_latitude column of data. If you want to index rows, you need to arguments of the form data[rows, columns], but if either rows or columns is missing, all will be assumed. So mtcars[1:3, ] is the first three rows of mtcars, with all columns. mtcars[mtcars['mpg'] <= 20 & mtcars['mpg'] >= 15, ] is the rows of mtcars that meet the logical requirements, with all columns.

There are also convenience functions that let you avoid retyping data$ or data[, e.g., this will do the same thing:

subset(data, pickup_latitude <=90 & pickup_latitude >=-90)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • How to slice for multiple columns as if `mpg` is been sliced above and I want to slice `cyl` ,`disp`, `hp`, `drat`, `wt`, `vs` as well togerther – surya rahul May 22 '19 at 12:47
  • Do you mean only select those columns? `mtcars[..same condition.., c("cyl", "disp", ...)]`. Or do you mean you have additional conditions? Just stick them together with `&`: `mtcars[mtcars['cyl'] == 4 & mtcars['disp'] < 300 & ..., ]` Use as many conditions on as many columns as you would like. – Gregor Thomas May 22 '19 at 12:49
  • How to do for a condition like `mpg between 18 to 20` ,`cyl between 6 to 10` , `disp between 10 to 50` ? i.e all columns having different range of values. @Gregor – surya rahul May 22 '19 at 13:44
  • 1
    Stick them together: `subset(mtcars, mpg >= 18 & mpg <= 20 & cyl >= 6 & cyl <= 10 & disp >= 100 & disp <= 300)`. You can use `&` for and, `|` for or... – Gregor Thomas May 22 '19 at 13:58
  • Thanks @Gregor This is working, I was using `|` instead `&` between two different columns to slice. – surya rahul May 22 '19 at 14:14