Create a variable capturing the most frequent occurence by group

前端 未结 3 752
不思量自难忘°
不思量自难忘° 2020-12-10 07:35

Define:

df1 <-data.frame(
id=c(rep(1,3),rep(2,3)),
v1=as.character(c(\"a\",\"b\",\"b\",rep(\"c\",3)))
)

s.t.

> df1
           


        
3条回答
  •  感情败类
    2020-12-10 07:57

    Another way consists of using tidyverse functions:

    • grouping first, using group_by(), and counting the occurrence of the second variable using tally()
    • arranging by the number of occurrences with arrange()
    • summarizing and picking out the first row with summarize() and first()

    Therefore:

    df1 %>%
    group_by(id, v1) %>%
    tally() %>%
    arrange(id, desc(n)) %>%
    summarize(freq = first(v1))
    

    This will give you just the mapping (which I find cleaner):

    # A tibble: 2 x 2
         id   freq
       
    1     1      b
    2     2      c
    

    You can then left_join your original data frame with that table.

提交回复
热议问题