Search code examples
rdataframesubsetslicedata-analysis

Slicing multiple columns in R


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 ?


Solution

  • 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)