【问题标题】:How to rename values by frequency in R如何在 R 中按频率重命名值
【发布时间】:2020-10-31 11:26:59
【问题描述】:

我正在根据来自 DAPC 的聚类数据制作几张图表。我需要所有图表的颜色相同,并且我想为最大的组使用特定的颜色。对于这个问题,重要的是我从 DAPC 获得了这样的数据集:

my_df <- data.frame(
  ID = c(1:10),
  Group = c("a", "b", "b", "c", "a", "b", "a", "b", "b", "c")
)

> my_df

ID  Group
1   a           
2   b           
3   b           
4   c           
5   a           
6   b           
7   a           
8   b           
9   b           
10  c

我知道如何找到这样的成员最多的组:

freqs <- table(my_df$Group)
freqs <- freqs[order(freqs, decreasing = TRUE)]

>freqs
b a c 
5 3 2 

有没有办法根据频率更改值?每次我重新运行 DAPC 时,它都会更改组,所以我想编写自动执行此操作的代码,而不必手动重做。以下是我希望更改数据框的方式:

> my_df                          > my_new_df
ID  Group                        ID  Group
1   a                             1  '2nd'
2   b                             2  '1st'          
3   b                             3  '1st'          
4   c                             4  '3rd'          
5   a                             5  '2nd'          
6   b                             6  '1st'          
7   a                             7  '2nd'          
8   b                             8  '1st'          
9   b                             9  '1st'          
10  c                             10 '3rd'          

【问题讨论】:

    标签: r rename frequency


    【解决方案1】:

    您可以使用ave 并使用相应的labels= 创建一个factor。为避免硬编码,请预先在向量lb 中定义标签。

    lb <- c("1st", "2nd", "3rd", paste0(4:10, "th"))
    
    with(my_df, factor(as.numeric(ave(as.character(Group), as.character(Group), FUN=table)),
           labels=rev(lb[1:length(unique(table(Group)))])))
    #  [1] 2nd 1st 1st 3rd 2nd 1st 2nd 1st 1st 3rd
    # Levels: 3rd 2nd 1st
    

    要转换更多这样的列,请使用sapply

    sapply(my_df[selected.columns], function(x) {
      factor(as.numeric(ave(as.character(x), as.character(x), FUN=table)),
             labels=rev(lb[1:length(unique(table(x)))]))
    })
    

    【讨论】:

    • 这适用于数字组,但如果 Group 是一个因素,我会得到这个错误:invalid factor level, NA generated number of items to replace is not a multiple of replacement length Error in factor(with(my_df2, ave(Group, Group, FUN = table)), labels = c("3rd", : invalid 'labels'; length 3 should be 1 or 0 相反,我找到了这个解决方法freqs &lt;- table(my_df$Group) freqs &lt;- freqs[order(freqs, decreasing = TRUE)] my_df$Group2 &lt;- factor(my_df$Group, levels = names(freqs), labels = c("1st", "2nd", "3rd")) 非常感谢!
    • @ZDinges 你说得对,在 R 版本 4.0.2 data.frame() 不再给出因素,所以我不知道(为了更好地共享数据,请始终使用 dput,这也复制了实际数据结构)。我调整了代码以处理字符和因子。您也可以自动构建标签,请参阅编辑!
    【解决方案2】:

    你的意思是这样的:

    my_df %>% left_join(my_df %>% group_by(Group) %>% summarise(N=n())) %>%
      arrange(desc(N)) %>% select(-N)
    
       ID Group
    1   2     B
    2   3     B
    3   6     B
    4   8     B
    5   9     B
    6   1     A
    7   5     A
    8   7     A
    9   4     C
    10 10     C
    

    更新

    这很有用。我希望这会有所帮助。

    my_df %>% left_join(my_df %>% group_by(Group) %>% summarise(N=n()) %>% arrange(desc(N)) %>%
                          bind_cols(my_df %>% select(Group) %>% distinct() %>% rename(key=Group)) %>%
                          rename(NewGroup=Group,Group=key)) %>%
      select(-c(Group,N)) %>% rename(Group=NewGroup)
    
       ID Group
    1   1     B
    2   2     A
    3   3     A
    4   4     C
    5   5     B
    6   6     A
    7   7     B
    8   8     A
    9   9     A
    10 10     C
    

    【讨论】:

    • 感谢您的回答。我正在尝试编辑值,使“B”变为“A”,因为它是最常见的。我将编辑我的问题,以便更清楚。
    • @ZDinges 我已经根据您的需要更新了答案。我希望这会有所帮助:)!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多