Why do you use '$" (the dollar sign) while adding multiple conditions in an R code?

Viewed 74

I was doing a course on R and I came across this line of code:

onlineta_city_hotels <- filter(hotel_bookings, 
                           (hotel=="City Hotel" & 
                             hotel_bookings$market_segment=="Online TA"))

This code did successfully produce the expected result. However, I tried removing the dollar sign and executed a new code:

onlineta_city_hotels <- filter(hotel_bookings, 
                           (hotel=="City Hotel" & 
                             market_segment=="Online TA"))

This code showed the same result. So I wanted to know the need for using a dollar sign in such statements.

1 Answers

Most of the tidyverse (and tidyverse includes dplyr) functions use Data masking. To quote from package authors-

.. provides data masking, which blurs the distinction between two types of variables:

  1. env-variables are "programming" variables and live in an environment. They are usually created with <-. Env-variables can be any type of R object.
  1. data-variables are "statistical" variables and live in a data frame. Data-variables live inside data frames, so must be vectors.

Now filter function comes from dplyr which uses data masking. So market_segment variable which live inside hotel_bookings can be called directly inside dplyr functions. This may not be the case always with base R functions.

hotel_bookings[hotel_bookings$hotel=="City Hotel" & 
                             hotel_bookings$market_segment=="Online TA", ]

The following will not work here

hotel_bookings[hotel=="City Hotel" & 
                             market_segment=="Online TA", ]

Thus inside all dplyr functions, variable names can be called by themselves (without using $).

For further reading/reference please see this page.

Related