2 codes, same logic but one doesn't work

why this code works :> unique(hotel_bookings$hotel)
and this doesnt:
gh ← hotel_bookings %>%

dataset link: /kaggle/input/hotel-booking/hotel_booking.csv

unique is a base R function.

If you want the tidyverse approach (your second code with the %>% pipe), you should use distinct() instead of unique.


So pipe operators cannot be used for base r functions?

I’m new to this so I don’t have much idea about this language.

Base R has its native pipe, which is |>

Here you have an explanation of the Differences between the base R and magrittr pipes: Differences between the base R and magrittr pipes

Hope that helps!

The logic is the same, but you are doing something different.

The first one, you are asking for the unique elements of the vector hotel that happens to be a column in the dataframe hotel_bookings

The second one, you are actually doing: unique(hotel_bookings,hotel) because the pipe will just place the previous object in the first argument position of the next function.

In the first one, you first take the vector (column) out of the dataframe via $, you can do this with pipes as well:

hotel_bookings %>%
    dplyr::pull(hotel) %>%

The dplyr function pull() extracts, pulls the vector/column hotel from the hotel_bookings data.frame.

Or, as yabellini said, you can do this in one line with distinct(), also a function from the dplyr package.