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

datazang

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?

slava-kohut

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]

編集
0

コメントを追加

0

関連記事

分類Dev

Spark Window - How to compare first row with nth row of a data frame?

分類Dev

How to get the data from the first cell in a UITableView?

分類Dev

Get the first displayed row of datagridview

分類Dev

How to get the first row values in sql server query

分類Dev

How to get the number of the first row of a range in OpenOffice Calc (BASIC)

分類Dev

How to get the first sentence from the first paragraph?

分類Dev

how to get first item from a group and prepare Data class object

分類Dev

How to get first match with sed?

分類Dev

How to get the first span tag?

分類Dev

How to get sheet that is not first sheet?

分類Dev

In same query MySql is possible to count row and get only the first row?

分類Dev

how to overwrite values of first row of dataframe

分類Dev

How to align first row of table with a line of text

分類Dev

How can I change the column names of a list to the first row of each data frame in a loop?

分類Dev

How can I row bind the unmatch data in the column of first table from the second table

分類Dev

How can I get the first 4 row values into one row in seperate columns for each visit_id record?

分類Dev

How to filter the first and the last row based on a condition in the last row in R

分類Dev

Return first matching row

分類Dev

Accessing first row in an object

分類Dev

Cannot get value for first column on a selected row in listview

分類Dev

jQuery Datatables get first displayed row custom attribute

分類Dev

subtract from first row in a data.table in R

分類Dev

Lopping duplicate the first and last row in pandas data frame

分類Dev

How to get the event of first option selected with jquery?

分類Dev

How to get the first tag containing a word with xpath?

分類Dev

How to get first two words from a string?

分類Dev

How to get average of first two numbers in array?

分類Dev

How to get the first occurrence of text inside a DIV

分類Dev

How to get the first occurrence ? regex python

Related 関連記事

  1. 1

    Spark Window - How to compare first row with nth row of a data frame?

  2. 2

    How to get the data from the first cell in a UITableView?

  3. 3

    Get the first displayed row of datagridview

  4. 4

    How to get the first row values in sql server query

  5. 5

    How to get the number of the first row of a range in OpenOffice Calc (BASIC)

  6. 6

    How to get the first sentence from the first paragraph?

  7. 7

    how to get first item from a group and prepare Data class object

  8. 8

    How to get first match with sed?

  9. 9

    How to get the first span tag?

  10. 10

    How to get sheet that is not first sheet?

  11. 11

    In same query MySql is possible to count row and get only the first row?

  12. 12

    how to overwrite values of first row of dataframe

  13. 13

    How to align first row of table with a line of text

  14. 14

    How can I change the column names of a list to the first row of each data frame in a loop?

  15. 15

    How can I row bind the unmatch data in the column of first table from the second table

  16. 16

    How can I get the first 4 row values into one row in seperate columns for each visit_id record?

  17. 17

    How to filter the first and the last row based on a condition in the last row in R

  18. 18

    Return first matching row

  19. 19

    Accessing first row in an object

  20. 20

    Cannot get value for first column on a selected row in listview

  21. 21

    jQuery Datatables get first displayed row custom attribute

  22. 22

    subtract from first row in a data.table in R

  23. 23

    Lopping duplicate the first and last row in pandas data frame

  24. 24

    How to get the event of first option selected with jquery?

  25. 25

    How to get the first tag containing a word with xpath?

  26. 26

    How to get first two words from a string?

  27. 27

    How to get average of first two numbers in array?

  28. 28

    How to get the first occurrence of text inside a DIV

  29. 29

    How to get the first occurrence ? regex python

ホットタグ

アーカイブ