Data Visualization - Creating Multiple Line Graphs, Screen 2

  1. In this code , may we use Sex==Male OR Sex==Female in filter()

life_expec_sex<-life_expec%>%filter(Race==‘All Races’& Sex!=‘Both Sexes’)

  1. When 2 columns are taken together , like in the above code, do we have to use ‘&’ operator and when 2 rows are taken together , do we use ‘OR’.

Hello @sharathnandalike,

Yes you can. The symbol for “OR” is |. Hence, the code will be:

  life_expec_sex <- life_expec %>% 
                        filter(Race == 'All Races' & (Sex == 'Male' | Sex == 'Female') )

Almost yes! …when two rows of the same column are taken together use ‘OR’. You can also use ‘OR’ to take two columns (or more) together. Here is an example.

Assume we have this dataframe.

df <- tibble(a = c(1,1,2,2,3,3), b = c(7,4,NA,7,4,5), c = c(NA,5,5,3,3,NA), d = c(9,1,1,1,9,NA))
df
# A tibble: 6 x 4
      a     b     c     d
  <dbl> <dbl> <dbl> <dbl>
1     1     7    NA     9
2     1     4     5     1
3     2    NA     5     1
4     2     7     3     1
5     3     4     3     9
6     3     5    NA    NA

We want to select the rows where at least one of the columns b, c or d is NA.

df %>%
    filter(is.na(b) | is.na(c) | is.na(d))
# A tibble: 3 x 4
      a     b     c     d
  <dbl> <dbl> <dbl> <dbl>
1     1     7    NA     9
2     2    NA     5     1
3     3     5    NA    NA

Best,
John.