Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 210001

How to get the first row that appears first in the data?

$
0
0

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?


Viewing all articles
Browse latest Browse all 210001

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>