对data.frame或matrix中的行求和

use*_*502 62 r

我有一个非常大的数据框,其中行作为观察,列作为遗传标记.我想创建一个新列,其中包含使用R的每个观察的选定列数的总和.

如果我有200列和100行,我想创建一个包含100行的新列,其中列为43到167列.列有1或0.新列包含每列的总和排,我将能够对具有最多遗传标记的个体进行排序.

我觉得这很接近:

data$new=sum(data$[,43:167])
Run Code Online (Sandbox Code Playgroud)

Gre*_*reg 85

您可以使用 rowSums

rowSums(data) 应该给你你想要的东西.

  • 对于OP问题`data $ new < - rowSums(data [43:167])` (11认同)
  • 为了节省一些人的时间,也许:避免与函数`rowsum`混淆,后者做了别的事情! (5认同)

Gre*_*now 26

rowSums函数(正如Greg提到的那样)会做你想要的,但你在答案中混合了子集化技术,在使用"[]"时不要使用"$",你的代码看起来应该更像:

data$new <- rowSums( data[,43:167] )
Run Code Online (Sandbox Code Playgroud)

如果你想使用除sum之外的函数,那么看看?申请在行或列中应用通用函数.


see*_*spi 7

我来到这里是希望找到一种方法来获取数据表中所有列的总和,并在实施上述解决方案时遇到问题。添加具有所有列总和的列的方法使用以下cbind函数:

cbind(data, total = rowSums(data))
Run Code Online (Sandbox Code Playgroud)

此方法total向数据添加一列,并避免了尝试使用上述解决方案对所有列求和时产生的对齐问题(有关此问题的讨论,请参阅下面的帖子)。

向矩阵错误添加新列


rub*_*a0x 6

只是为了完整性。我将列出此处未提及的其他方法,这是使用 dplyr 语法和矩阵执行相同操作的不同方法:

mat = matrix(1:12, ncol = 3)

library(dplyr)

mat %>% as_tibble() %>% 
   mutate(sum = rowSums(across(where(is.numeric))))

# A tibble: 4 x 4
     V1    V2    V3   sum
  <int> <int> <int> <dbl>
1     1     5     9    15
2     2     6    10    18
3     3     7    11    21
4     4     8    12    24
Run Code Online (Sandbox Code Playgroud)

或 c_across:

mat %>% as_tibble() %>%
  rowwise() %>% 
  mutate(sumrange = sum(c_across(), na.rm = T))
Run Code Online (Sandbox Code Playgroud)

或按列名称选择特定列:

mat %>% as_tibble() %>%
    mutate( 'B1' = V1, B2 = V2) %>% 
    rowwise() %>% 
    mutate(sum_startswithB = 
sum(c_across(starts_with("B")), na.rm = T))

     V1    V2    V3    B1    B2 sum_startswithx
  <int> <int> <int> <int> <int>           <int>
1     1     5     9     1     5               6
2     2     6    10     2     6               8
3     3     7    11     3     7              10
4     4     8    12     4     8              12 
Run Code Online (Sandbox Code Playgroud)

在本例中按列索引为第一列到第四列:

mat %>% as_tibble() %>%
  mutate( 'B1' = V1, B2 = V2) %>%
  rowwise() %>% 
  mutate(SumByIndex = sum(c_across(c(1:4)), na.rm = T))

     V1    V2    V3    B1    B2 SumByIndex
  <int> <int> <int> <int> <int>      <int>
1     1     5     9     1     5         16
2     2     6    10     2     6         20
3     3     7    11     3     7         24
4     4     8    12     4     8         28
Run Code Online (Sandbox Code Playgroud)

使用正则表达式:

mat %>% as_tibble() %>%
  mutate( 'B1' = V1, B2 = V2) %>%
  mutate(sum_V = rowSums(.[grep("V[2-3]", names(.))], na.rm = TRUE),
  sum_B = rowSums(.[grep("B", names(.))], na.rm = TRUE))

     V1    V2    V3    B1    B2 sum_V sum_B
  <int> <int> <int> <int> <int> <dbl> <dbl>
1     1     5     9     1     5    14     6
2     2     6    10     2     6    16     8
3     3     7    11     3     7    18    10
4     4     8    12     4     8    20    12
Run Code Online (Sandbox Code Playgroud)

使用应用函数更方便,因为您可以跨列选择总和、平均值、最大值、最小值、方差和标准差。

mat %>% as_tibble() %>%
  mutate( 'B1' = V1, B2 = V2) %>%
  mutate(sum = select(., V1:B1) %>% apply(1, sum, na.rm=TRUE)) %>%
  mutate(mean = select(., V1:B1) %>% apply(1, mean, na.rm=TRUE)) %>%
  mutate(max = select(., V1:B1) %>% apply(1, max, na.rm=TRUE)) %>%
  mutate(min = select(., V1:B1) %>% apply(1, min, na.rm=TRUE)) %>%  
  mutate(var = select(., V1:B1) %>% apply(1, var, na.rm=TRUE)) %>%
  mutate(sd = select(., V1:B1) %>% apply(1, sd, na.rm=TRUE))

     V1    V2    V3    B1    B2   sum  mean   max   min   var    sd
  <int> <int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <dbl>
1     1     5     9     1     5    16     4     9     1  14.7  3.83
2     2     6    10     2     6    20     5    10     2  14.7  3.83
3     3     7    11     3     7    24     6    11     3  14.7  3.83
4     4     8    12     4     8    28     7    12     4  14.7  3.83
Run Code Online (Sandbox Code Playgroud)

注意:var 和 sd 相同的输出不是错误,因为数据是线性生成的,1:12您可以验证计算第一列的值:

> sd(c(1,5,9,1))
[1] 3.829708
> sd(c(2,6,10,2))
[1] 3.829708
Run Code Online (Sandbox Code Playgroud)