【问题标题】:R: Sort data by most common value of a columnR:按列的最常见值对数据进行排序
【发布时间】:2020-11-06 10:20:13
【问题描述】:

我在这里关注这个 stackoverflow 帖子:Sort based on Frequency in R

我正在尝试按“Node_A”列的最常见值对我的数据进行排序。

library(dplyr)

Data_I_Have <- data.frame(
   
    "Node_A" = c("John", "John", "John", "John, "John", "Peter", "Tim", "Kevin", "Adam", "Adam", "Xavier"),
    "Node_B" = c("Claude", "Peter", "Tim", "Tim", "Claude", "Henry", "Kevin", "Claude", "Tim", "Henry", "Claude"),
    " Place_Where_They_Met" = c("Chicago", "Boston", "Seattle", "Boston", "Paris", "Paris", "Chicago", "London", "Chicago", "London", "Paris"),
  "Years_They_Have_Known_Each_Other" = c("10", "10", "1", "5", "2", "8", "7", "10", "3", "3", "5"),
  "What_They_Have_In_Common" = c("Sports", "Movies", "Computers", "Computers", "Video Games", "Sports", "Movies", "Computers", "Sports", "Sports", "Video Games")
)

sort = Data_I_Have %>% arrange(Node_A, desc(Freq))

有人可以告诉我我做错了什么吗? 谢谢

【问题讨论】:

    标签: r sorting dplyr


    【解决方案1】:

    在对数据进行排序之前,您需要对数据进行计数。你可以试试:

    library(dplyr)
    
    Data_I_Have %>%
      count(Node_A, sort = TRUE) %>%
      left_join(Data_I_Have, by = 'Node_A')
    
    #   Node_A n Node_B X.Place_Where_They_Met Years_They_Have_Known_Each_Other What_They_Have_In_Common
    #1    John 5 Claude                Chicago                               10                   Sports
    #2    John 5  Peter                 Boston                               10                   Movies
    #3    John 5    Tim                Seattle                                1                Computers
    #4    John 5    Tim                 Boston                                5                Computers
    #5    John 5 Claude                  Paris                                2              Video Games
    #6    Adam 2    Tim                Chicago                                3                   Sports
    #7    Adam 2  Henry                 London                                3                   Sports
    #8   Kevin 1 Claude                 London                               10                Computers
    #9   Peter 1  Henry                  Paris                                8                   Sports
    #10    Tim 1  Kevin                Chicago                                7                   Movies
    #11 Xavier 1 Claude                  Paris                                5              Video Games
    

    或者我们可以使用add_count 代替count,这样我们就不必加入数据。

    Data_I_Have %>% add_count(Node_A, sort = TRUE)
    

    如果不需要,您可以从最终输出中删除 n 列。

    【讨论】:

    • 感谢您的帮助,我成功了!并感谢您对我的另一篇文章的评论(我想我错误地标记了它,对不起)。我在移动设备上并且是 stackexchange 的新手,我不知道您可以“接受一个答案作为官方答案”。再次感谢您的所有帮助!
    【解决方案2】:

    作为您提到的帖子的最后一个答案:

    Data_I_Have %>% 
      group_by(Node_A) %>%
      arrange( desc(n()))
    
    # Node_A Node_B X.Place_Where_They_Met Years_They_Have_Known_Each_Other What_They_Have_In_Common
    # <chr>  <chr>  <chr>                  <chr>                            <chr>                   
    # 1  John   Claude Chicago                10                               Sports                  
    # 2  John   Peter  Boston                 10                               Movies                  
    # 3  John   Tim    Seattle                1                                Computers               
    # 4  John   Tim    Boston                 5                                Computers               
    # 5  John   Claude Paris                  2                                Video Games             
    # 6  Peter  Henry  Paris                  8                                Sports                  
    # 7  Tim    Kevin  Chicago                7                                Movies                  
    # 8  Kevin  Claude London                 10                               Computers               
    # 9  Adam   Tim    Chicago                3                                Sports                  
    # 10 Adam   Henry  London                 3                                Sports                  
    # 11 Xavier Claude Paris                  5                                Video Games             
    

    【讨论】:

    • 谢谢,这成功了!我必须将生成的 tibble 转换为 data.frame
    猜你喜欢
    • 2021-10-29
    • 2022-01-09
    • 2019-10-14
    • 2015-06-21
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    相关资源
    最近更新 更多