Apologies Newbie here, Q&A "what's the need for the $ symbol in this code chunk example and others?"

"what's the need for the $ symbol in this code chunk example and others?"

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

Assuming that the filter function is the one in the dplyr package and that the asterisks were added just for emphasis in your post, there is no need for the $. The code should be

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

The $ is used to get a specific column from a data frame. If you wanted to store the values of the market_segment column in a separate vector, you could write

NewVec <- hotel_bookings$market_segment

Within the filter function, you give the name of the data frame as the first argument, as seen in your code, and then the column names can be used as bare names with no quotes and no explicit reference to the data frame in the subsequent arguments of the function.

1 Like

Thank you very much!

Your explanation really cleared things up for me.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.