help with order of piping

I am confused by the order in which I would arrange these two lines with pipes to make the code more readable (and also, this doesn't really need two lines as it is now, obviously). I don't have the piping mindset, yet, and it's hard in the beginning.

I have two tibbles, "ho" and "gw". Both tibbles have the column "S". I want to find the unique elements in "ho" missing in "gw" (honig, "ho not in gw") and store them in order.

honig <- unique(ho$S[!(ho$S %in% gw$S)])
honig <- honig[order(honig)]

I guess I would start with the data and select the column, but then I am already glitching.

If the tibbles have a shared column, you could perform an anti_join() to find elements from the first tibble that do not exist in the second like so:

ho %>% anti_join(gw, by = "S")

Once you have that subset, you can call distinct() to get unique values and arrange them in whatever order you need.

1 Like

Improvement for @siddharthprabhu

ho %>%
anti_join(gw, by = "S") %>%
distinct(S, .keep_all = T)

alright, thanks. I will have to try how to incorporate order() into that pipe sequence. It's still not clear where I need to place the dots and the brackets and functions and dataframes. This is almost perfect, though.

1 Like

Consider using the dplyr::arrange() instead of order() if your intention is just to sort. It's much easier to use within pipes.

1 Like

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