Summarise over all columns

前端 未结 3 1614
星月不相逢
星月不相逢 2020-12-17 19:31

I have data of the following format:

gen = function () sample.int(10, replace = TRUE)
x = data.frame(A = gen(), C = gen(), G = gen(), T = gen())
3条回答
  •  遥遥无期
    2020-12-17 19:51

    I once did something similar, and by that time I ended up with:

    x %>%
      rowwise() %>%
      do(data.frame(., res = sum(unlist(.))))
    #    A  C G  T res
    # 1  3  2 8  6  19
    # 2  6  1 7 10  24
    # 3  4  8 6  7  25
    # 4  6  4 7  8  25
    # 5  6 10 7  2  25
    # 6  7  1 2  2  12
    # 7  5  4 8  5  22
    # 8  9  2 3  2  16
    # 9  3  4 7  6  20
    # 10 7  5 3  9  24
    

    Perhaps your more complex function works fine without unlist, but it seems like it is necessary for sum. Because . refers to the "current group", I initially thought that . for e.g. the first row in the rowwise machinery would correspond to x[1, ], which is a list, which sum swallows happily outside do

    is.list((x[1, ]))
    # [1] TRUE
    
    sum(x[1, ])
    # [1] 19 
    

    However, without unlist in do an error is generated, and I am not sure why:

    x %>%
      rowwise() %>%
      do(data.frame(., res = sum(.)))
    # Error in sum(.) : invalid 'type' (list) of argument
    

提交回复
热议问题