sum columns with similar names in R

前端 未结 4 1873
梦谈多话
梦谈多话 2021-01-14 17:02

I\'ve got a dataframe that\'s got lots of columns that are something like this:

data <- data.frame (a.1 = 1:5, a.2b = 3:7, a.5 = 5:9, bt.16 = 4:8, bt.1234         


        
相关标签:
4条回答
  • 2021-01-14 17:27

    You can try

    library(tidyverse)
    data.frame (a.1 = 1:5, a.2b = 3:7, a.5 = 5:9, bt.16 = 4:8, bt.12342 = 7:11) %>% 
      rownames_to_column() %>% 
      gather(k, v, -rowname) %>% 
      separate(k, letters[1:2]) %>% 
      group_by(rowname, a) %>% 
      summarise(Sum=sum(v)) %>% 
      spread(a, Sum)
    #> # A tibble: 5 x 3
    #> # Groups:   rowname [5]
    #>   rowname     a    bt
    #>   <chr>   <int> <int>
    #> 1 1           9    11
    #> 2 2          12    13
    #> 3 3          15    15
    #> 4 4          18    17
    #> 5 5          21    19
    

    Created on 2018-04-16 by the reprex package (v0.2.0).

    0 讨论(0)
  • 2021-01-14 17:31

    Here's another tidyverse solution:

    library(tidyverse)
    
    t(data) %>%
      data.frame() %>%
      group_by(., id = gsub('\\..*', '', rownames(.))) %>%
      summarise_all(sum) %>%
      data.frame() %>%
      column_to_rownames(var = 'id') %>%
      t()
    

    Result:

        a bt
    X1  9 11
    X2 12 13
    X3 15 15
    X4 18 17
    X5 21 19
    
    0 讨论(0)
  • 2021-01-14 17:32

    Here a solution with base R:

    > prefixes = unique(sub("\\..*", "", colnames(data)))
    > sapply(prefixes, function(x)rowSums(data[,startsWith(colnames(data), x)]))
          a bt
    [1,]  9 11
    [2,] 12 13
    [3,] 15 15
    [4,] 18 17
    [5,] 21 19
    
    0 讨论(0)
  • 2021-01-14 17:42
    data <- data.frame (a.1 = 1:5, a.2b = 3:7, a.5 = 5:9, bt.16 = 4:8, bt.12342 = 7:11)
    i <- grepl("a.", names(data), fixed = TRUE)
    result <- data.frame(a=rowSums(data[, i]), bt=rowSums(data[, !i]))
    result
    # > result
    #    a bt
    # 1  9 11
    # 2 12 13
    # 3 15 15
    # 4 18 17
    # 5 21 19
    

    If you have more than two prefixes you can do something like:

    prefs <- c("a.", "bt.")
    as.data.frame(lapply(prefs, function(p) rowSums(data[, grepl(p, names(data), fixed = TRUE)]) ))
    
    0 讨论(0)
提交回复
热议问题