【问题标题】:Adding equivalent columns of relevant data in R在 R 中添加相关数据的等效列
【发布时间】:2018-04-25 05:06:49
【问题描述】:

我已经使用 table() 从原始数据创建了一个分类标签表。

我想添加相关标签的频率/计数。例如`

S/n No.     Tags    Frequency/Count

1      Problem 1      56325
2      Problem 2      11233
3      Problem 3      546321
4      Problem 1      2123345 
       & Problem 2      
5      Problem 2      9657531
       & Problem 3
6      Problem 1      623589542
       & Problem 2
       & Problem 3 ` 

现在我希望输出是这样的,

S/n no.  Tagging     Freq/Count

1        Problem 1  (56325+2123345+623589542)=625769212
2        Problem 2  (11233+2123345+9657531+623589542)=635381651
3        Problem 3  (546321+9657531+623589542)=633793394

注意:() 内的数据不会显示在输出中。

现在,我有 78 种不同类型的标记关键字。表中有大约 250 行。

@Maurits Evers 和@akrun 正确回答了这个问题。 您需要为此安装 tidyverse 软件包。 请输入

install.packages("tidyverse")

如果您没有安装 ,请在您的 R 控制台中。 访问tidyverse 网站了解更多信息。

【问题讨论】:

  • 感谢您正确编辑帖子@Suren。我遇到了多个错误,这就是为什么我把整个事情都放在引号里。抱歉,我是 Stackoverflow 格式的新手。

标签: tidyverse r data.table tidyverse


【解决方案1】:

使用tstrsplit 的data.table 解决方案

library(data.table)
setDT(df)[,.(Tags = unlist(tstrsplit(Tags, " & ", fixed = TRUE)), # Split by &
             Freq = Frequency_Count) # Take the Frequency_Count too
          ][!is.na(Tags), # ignore non-matches
            .(Freq_Count = sum(Freq)), # sum frequencies
            by = Tags] # by the splitted tags
#         Tags Freq_Count
# 1: Problem 1  625769212
# 2: Problem 2  635381651
# 3: Problem 3  633793394

【讨论】:

    【解决方案2】:

    请注意,在您的示例中,在汇总计数时,S/n no. 列中的条目似乎被忽略了;您没有提供任何详细信息,因此我将忽略此列中的条目。

    我们可以使用strsplit 分隔条目,然后使用unnest 并在summariseFrequency/Count 中的summarise'ing 值之前按Tags 分组行:

    library(tidyverse);
    df %>%
        mutate_if(is.factor, as.character) %>%
        select(-SN_No) %>%
        mutate(Tags = strsplit(Tags, " & ")) %>%
        unnest() %>%
        group_by(Tags) %>%
        summarise(Freq_Count = sum(Frequency_Count))
    ## A tibble: 3 x 2
    #  Tags      Freq_Count
    #  <chr>          <int>
    #1 Problem 1  625769212
    #2 Problem 2  635381651
    #3 Problem 3  633793394  
    

    样本数据

    df <- read.table(text =
        "'SN_No'     Tags    'Frequency_Count'
    1      'Problem 1'      56325
    2      'Problem 2'      11233
    3      'Problem 3'      546321
    4      'Problem 1 & Problem 2'     2123345
    5      'Problem 2 & Problem 3'      9657531
    6      'Problem 1 & Problem 2 & Problem 3'      623589542", header = T)
    

    【讨论】:

    • 感谢@Maurits Evers 的帮助。这工作得很好。
    【解决方案3】:

    这是separate_rows的一个选项

    library(tidyverse)
    df1 %>% 
      separate_rows(Tags, sep = "\\s+&\\s+") %>% 
      group_by(Tags) %>% 
      summarise(SN_No = first(SN_No), Frequency_Count = sum(Frequency_Count)) %>%
      select(names(df1))
    # A tibble: 3 x 3
    #    SN_No Tags      Frequency_Count
    #   <int> <chr>               <int>
    #1     1 Problem 1       625769212
    #2     2 Problem 2       635381651
    #3     3 Problem 3       633793394
    

    数据

    df1 <- structure(list(SN_No = 1:6, Tags = structure(c(1L, 4L, 6L, 2L, 
     5L, 3L), .Label = c("Problem 1", "Problem 1 & Problem 2", 
      "Problem 1 & Problem 2 & Problem 3", 
     "Problem 2", "Problem 2 & Problem 3", "Problem 3"), class = "factor"), 
    Frequency_Count = c(56325L, 11233L, 546321L, 2123345L, 9657531L, 
    623589542L)), .Names = c("SN_No", "Tags", "Frequency_Count"
     ), class = "data.frame", row.names = c(NA, -6L))
    

    【讨论】:

    • 再次感谢@akrun 的帮助。这段代码正确,通俗易懂。
    【解决方案4】:

    假设您的意思是 Problem1 和 Problem 2 ,因为频率都是 2123345,我读取您的数据如下,并使用聚合函数来获得我认为您想要的结果:

    table1 <- read.table(text = '
      Tags    FrequencyCount
      Problem1      56325
      Problem2      11233
      Problem3      546321
      Problem1      2123345 
      Problem2     2123345
      Problem2     9657531
      Problem3     9657531
      Problem1     623589542
      Problem2     623589542
      Problem3     623589542',
                     header = TRUE) 
    
    
    aggregate(FrequencyCount ~ Tags, table1, sum)
    
          Tags FrequencyCount
    1 Problem1      625769212
    2 Problem2      635381651
    3 Problem3      633793394
    

    如果您缺少需要填写的值,如下例所示,您可以先复制以前的值:

    table1 <- read.table(text = '
      Tags    FrequencyCount
      Problem1      56325
      Problem2      11233
      Problem3      546321
      Problem1      2123345 
      Problem2     NA
      Problem2     9657531
      Problem3     NA
      Problem1     623589542
      Problem2     NA
      Problem3     NA',
                     header = TRUE) 
    
    library(data.table)
    while(sum(is.na(table1$FrequencyCount)) > 0){
    table1$FrequencyCount <- ifelse(is.na(table1$FrequencyCount), 
    shift(table1$FrequencyCount), table1$FrequencyCount)
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 2022-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多