Search code examples
rdataframeoperator-keywordnotation

Using logical operators in R, A quick question


I am working with a data set in R called "USArrests." I want to obtain those states whose murder rate is >8, assault rate >170, and rape rate >21.

            Murder Assault UrbanPop Rape         states
Alabama          13.2     236       58 21.2        Alabama
Alaska           10.0     263       48 44.5         Alaska
Arizona           8.1     294       80 31.0        Arizona
Arkansas          8.8     190       50 19.5       Arkansas
California        9.0     276       91 40.6     California
Florida          15.4     335       80 31.9        Florida
Georgia          17.4     211       60 25.8        Georgia
Illinois         10.4     249       83 24.0       Illinois
Kentucky          9.7     109       52 16.3       Kentucky
Louisiana        15.4     249       66 22.2      Louisiana
Maryland         11.3     300       67 27.8       Maryland
Michigan         12.1     255       74 35.1       Michigan
Mississippi      16.1     259       44 17.1    Mississippi
Missouri          9.0     178       70 28.2       Missouri
Nevada           12.2     252       81 46.0         Nevada
New Mexico       11.4     285       70 32.1     New Mexico
New York         11.1     254       86 26.1       New York
North Carolina   13.0     337       45 16.1 North Carolina
South Carolina   14.4     279       48 22.5 South Carolina
Tennessee        13.2     188       59 26.9      Tennessee
Texas            12.7     201       80 25.5          Texas
Virginia          8.5     156       63 20.7       Virginia
NA                 NA      NA       NA   NA           <NA>
NA.1               NA      NA       NA   NA           <NA>
NA.2               NA      NA       NA   NA           <NA>
NA.3               NA      NA       NA   NA           <NA>
NA.4               NA      NA       NA   NA           <NA>
NA.5               NA      NA       NA   NA           <NA>
NA.6               NA      NA       NA   NA           <NA>
NA.7               NA      NA       NA   NA           <NA>
NA.8               NA      NA       NA   NA           <NA>
NA.9               NA      NA       NA   NA           <NA>
NA.10              NA      NA       NA   NA           <NA>
NA.11              NA      NA       NA   NA           <NA>
NA.12              NA      NA       NA   NA           <NA>
NA.13              NA      NA       NA   NA           <NA>
NA.14              NA      NA       NA   NA           <NA>
NA.15              NA      NA       NA   NA           <NA>
NA.16              NA      NA       NA   NA           <NA>
NA.17              NA      NA       NA   NA           <NA>
NA.18              NA      NA       NA   NA           <NA>
NA.19              NA      NA       NA   NA           <NA>
NA.20              NA      NA       NA   NA           <NA>
NA.21              NA      NA       NA   NA           <NA>
NA.22              NA      NA       NA   NA           <NA>
NA.23              NA      NA       NA   NA           <NA>
NA.24              NA      NA       NA   NA           <NA>
NA.25              NA      NA       NA   NA           <NA>
NA.26              NA      NA       NA   NA           <NA>
NA.27              NA      NA       NA   NA           <NA>
NA.28              NA      NA       NA   NA           <NA>
NA.29              NA      NA       NA   NA           <NA>
NA.30              NA      NA       NA   NA           <NA>
NA.31              NA      NA       NA   NA           <NA>
NA.32              NA      NA       NA   NA           <NA>
NA.33              NA      NA       NA   NA           <NA>
NA.34              NA      NA       NA   NA           <NA>
NA.35              NA      NA       NA   NA           <NA>
NA.36              NA      NA       NA   NA           <NA>
NA.37              NA      NA       NA   NA           <NA>
NA.38              NA      NA       NA   NA           <NA>
NA.39              NA      NA       NA   NA           <NA>
NA.40              NA      NA       NA   NA           <NA>
NA.41              NA      NA       NA   NA           <NA>
NA.42              NA      NA       NA   NA           <NA>
NA.43              NA      NA       NA   NA           <NA>
NA.44              NA      NA       NA   NA           <NA>

This was my code

USArrests.1=USArrests[c(USArrests$Murder>8, USArrests$Assault>170, USArrests$Rape>21),].

My question is, what is the more effective way to write this code so it does not have the "NA" rows? I know I can use the "&" operator but it only works for two and not three? Thanks


Solution

  • It appears you are concatenating your logical operators instead of evaluating them with the & “and” statement.

    conditions <- USArrests$Murder>8 & USArrests$Assault>170 & USArrests$Rape >21
    USArrests[conditions,]
    

    notice that conditions is a series of TRUE and FALSEs. Alternatively, one can use which() to return the index (rows) which will have the exact same result but may be useful to see:

    which(conditions)
    #  [1]  1  2  3  5  9 10 13 18 20 22 25 28 31 32 40 42 43
    USArrests[conditions,]
    

    Some packages make subsetting more intuitive, such as the data.table package.

    #install.packages("data.table") #uncomment to install
    library(data.table)
    USArrests_dt <- as.data.table(USArrests)
    USArrests_dt[Murder>8 & Assault>170 & Rape > 21]
    

    which is better for very large datasets and has a convenient print setting. It's a little more complex than your regular data.frame which you should master first.