0

Here is the question: Can you use the across() function and the starts_with() selector?

This is what I have:

pm %>% 
 dplyr::filter(city == c("Baltimore", 
"Albuquerque")) %>% 
select(starts_with("county"), city)

This is what I am getting: I keep on getting two rows for Baltimore. The rows are the exact same and only want one

[[county, county_area, county_pop, city],
[Baltimore (City), 209..., 620.., Baltimore],
[Baltimore (City), 209..., 620.., Baltimore],
[Bernalillo, 300..., 662.., Albuquerque]]
kzzz
  • 1
  • 1
  • First you need to have `city %in% c("Baltimore", "Albuquerque")` instead of `==`. Also I think there is nothing otherwise wrong with your code causing you to get two rows for Baltimore. The `pm` dataframe seems to just have two rows for Baltimore. – qdread Mar 17 '22 at 13:25
  • I only want one per city though. Both entries for Baltimore are the exact same. – kzzz Mar 17 '22 at 13:35
  • 1
    The `duplicated` function can be used to find (and then remove) duplicates. For example, `pm2 <- pm[!duplicated(pm), ]` will return only unique rows. – sashahafner Mar 17 '22 at 13:59
  • If you want a "tidyverse" solution there's https://stackoverflow.com/a/26302351/2854608 which uses the `distinct()` function to keep only unique rows. Here that would be `pm %>% distinct(city, county, .keep_all = TRUE)` or something similar depending on how many columns you want to do the distinct operation on. – qdread Mar 17 '22 at 15:22

0 Answers0