划分2行以创建新行

问题描述 投票:0回答:1

非常简单的问题,已被问到before,但我不明白我得到的错误。

我想要做的就是在表格中划分两行(而不是数据帧)并在底部创建一个新行:

           a     b       c       d           e   Total
 A         460      1063    1680    2535     76  5814
 B         554      4974    3052    7094     239 15913
 Total     1014     6037    4732    962      315 21727

    table[4,] <- table[1,] / table[3,] 

Error in `[<-`(`*tmp*`, 4, , value = ...  : 
  subscript out of bounds
r
1个回答
0
投票

通过示例使用iris数据集添加第1行和第2行:

attach(iris)
tab<-table(iris$Species,iris$Petal.Width)
new_row <- tab[1,] + tab[2,]
tab<-rbind(tab,new_row)

结果:

> tab
           0.1 0.2 0.3 0.4 0.5 0.6 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5
setosa       5  29   7   7   1   1 0   0   0   0   0   0   0   0   0   0 0   0   0   0   0   0
versicolor   0   0   0   0   0   0 7   3   5  13   7  10   3   1   1   0 0   0   0   0   0   0
virginica    0   0   0   0   0   0 0   0   0   0   1   2   1   1  11   5 6   6   3   8   3   3
new_row      5  29   7   7   1   1 7   3   5  13   7  10   3   1   1   0 0   0   0   0   0   0
© www.soinside.com 2019 - 2024. All rights reserved.