向显示幅度的数据帧添加一行(最大值为正值或负值之和)

时间:2021-07-03 01:31:15

I'd like to insert a last row to a dataframe that would show either the sum of the positive values or the sum of the negative value per column, whichever sum has the greatest amplitude.

我想在数据帧中插入最后一行,该数据帧将显示正值之和或每列负值之和,无论哪个总和具有最大幅度。

The final table would look like this with the last row ("MaxAmplitude") being the result I would like to obtain:

最后一个表看起来像这样,最后一行(“MaxAmplitude”)是我想要获得的结果:

  c("A", "B", "C", "Max Amplitude")  X1  X2  X3  X4  X5
1                                 A -30  10  10  -2   8
2                                 B   5  50 100 -10 -10
3                                 C -10 -30   1  -1   3
4                     Max Amplitude -40  60 111 -13  11

My idea was a multiple step approach, where I would successively add several rows:

我的想法是一个多步骤的方法,我会连续添加几行:

  • 1st row making the sum of the positive values
  • 第一行是正值的总和

  • 2nd row making the sum of the negative values
  • 第二行是负值的总和

  • 3rd row doing the test of whichever of the row 1 or 2 has the greatest amplitude by comparing the absolute value of both sums.
  • 通过比较两个和的绝对值,对第1行或第2行中的任何一个进行测试的第3行具有最大幅度。

My issue is that I'd like to do it without having to add that many rows to the dataframe. Besides, I don't know how I could do to diplay the negative sign in case the sum of negative values has the greatest amplitude (I would lose this information if I compare absolute value).

我的问题是我想这样做而不必向数据帧添加那么多行。此外,我不知道如何在负值的总和具有最大幅度的情况下显示负号,如果我比较绝对值,我将丢失此信息。

Would someone have any solution as to how I could code this please? Thanks a lot for your help!

有人可以解决我如何编码这个问题吗?非常感谢你的帮助!

df <- cbind(c("A","B","C"),data.frame(matrix(c(-30,5,-10,
                                                       10,50,-40,
                                                       10,100,1,
                                                       -2,-10,-1,
                                                       8,-10,3
                                                       ), nrow = 3, ncol = 5)))

1 个解决方案

#1


2  

Is this what you want ? Also , apply return a matrix not data.frame

这是你想要的吗 ?另外,应用返回矩阵而不是data.frame

apply(df[,2:6],2,function(x) sum(x[which((x>0)==(sum(x)>0))]))


 X1  X2  X3  X4  X5 
-40  60 111 -13  11 

Just df[nrow(df)+1,]=c('Max Amplitude',c(apply(df[,2:6],2,function(x) sum(x[which((x>0)==(sum(x)>0))]))))

只是df [nrow(df)+1,] = c('Max Amplitude',c(apply(df [,2:6],2,function(x)sum(x [which((x> 0)== (总和(X)> 0))]))))

> df
  c("A", "B", "C")  X1  X2  X3  X4  X5
1                A -30  10  10  -2   8
2                B   5  50 100 -10 -10
3                C -10 -40   1  -1   3
4    Max Amplitude -40  60 111 -13  11

#1


2  

Is this what you want ? Also , apply return a matrix not data.frame

这是你想要的吗 ?另外,应用返回矩阵而不是data.frame

apply(df[,2:6],2,function(x) sum(x[which((x>0)==(sum(x)>0))]))


 X1  X2  X3  X4  X5 
-40  60 111 -13  11 

Just df[nrow(df)+1,]=c('Max Amplitude',c(apply(df[,2:6],2,function(x) sum(x[which((x>0)==(sum(x)>0))]))))

只是df [nrow(df)+1,] = c('Max Amplitude',c(apply(df [,2:6],2,function(x)sum(x [which((x> 0)== (总和(X)> 0))]))))

> df
  c("A", "B", "C")  X1  X2  X3  X4  X5
1                A -30  10  10  -2   8
2                B   5  50 100 -10 -10
3                C -10 -40   1  -1   3
4    Max Amplitude -40  60 111 -13  11