I have a data set shown as below:
data <- tribble(
~shop_name, ~products, ~category_name,
"A", 1, "Game",
"A", 1, "Book",
"A", 2, "Electronic",
"A", 3, "Home",
"B", 5, "Game",
"B", 5, "Electronic",
"B", 8, "Home",
"C", 1, "Book",
"C", 7, "Game",
"C", 9, "Game",
)
I wanted to see the top 1 category based on the products, and coded this:
data %>%
group_by(shop_name) %>%
top_n(1, products) %>%
mutate(top_category = toString(category_name))
But because products have sometimes same values per each shop_name, there are more than one category names in the "top_category". How can I get the first row that appears first in the dataset?
Use dplyr::first
:
data %>%
group_by(shop_name) %>%
summarise(products = first(products),
category_name = first(category_name))
To keep all columns without explicitly specifying them
data %>%
group_by(shop_name) %>%
summarise_all(first)
Output
# shop_name products category_name
# <chr> <dbl> <chr>
# 1 A 1 Game
# 2 B 5 Game
# 3 C 1 Book
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加