r 中 apply 函数的问题:它仅应用于第一列
Problem with apply function in r: it's applied only in the first column
我有一个数据帧调用索引,它是子集的结果:
X1 X2 X3 X4 X5 X6 X7 X8 X9
1 201860304 677673878 43255779 158604525 72092966 28837186 57674373 57674373 144185931
145 205140086 688684575 43958590 161181496 73264317 29305727 58611453 58611453 146528633
291 213047739 715231695 45653087 167394652 76088478 30435391 60870783 60870783 152176956
442 220164438 739123472 47178094 172986345 78630157 31452063 62904125 62904125 157260313
594 227189443 762707415 48683452 178505991 81139087 32455635 64911269 64911269 162278173
首先不知道为什么子集后行号显示的是“1,145,291...”而不是“1,2,3...”。
二、当我使用
indices <- as.data.frame(apply(indices, 2, function(y) 100 * y / y[1]))
结果是:
X1 X2 X3 X4 X5 X6 X7 X8 X9
1 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000
145 101.6248 101.6248 101.6248 101.6248 101.6248 101.6248 101.6248 101.6248 101.6248
291 105.5422 105.5422 105.5422 105.5422 105.5422 105.5422 105.5422 105.5422 105.5422
442 109.0677 109.0677 109.0677 109.0677 109.0677 109.0677 109.0677 109.0677 109.0677
重复同一列。
我该如何解决这个问题?
谢谢!
如果我们需要重置行名称
row.names(indices) <- NULL
另外,我们可以用向量化的方式来做到这一点
100 *indices/indices[1,][col(indices)]
我有一个数据帧调用索引,它是子集的结果:
X1 X2 X3 X4 X5 X6 X7 X8 X9
1 201860304 677673878 43255779 158604525 72092966 28837186 57674373 57674373 144185931
145 205140086 688684575 43958590 161181496 73264317 29305727 58611453 58611453 146528633
291 213047739 715231695 45653087 167394652 76088478 30435391 60870783 60870783 152176956
442 220164438 739123472 47178094 172986345 78630157 31452063 62904125 62904125 157260313
594 227189443 762707415 48683452 178505991 81139087 32455635 64911269 64911269 162278173
首先不知道为什么子集后行号显示的是“1,145,291...”而不是“1,2,3...”。
二、当我使用
indices <- as.data.frame(apply(indices, 2, function(y) 100 * y / y[1]))
结果是:
X1 X2 X3 X4 X5 X6 X7 X8 X9
1 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000
145 101.6248 101.6248 101.6248 101.6248 101.6248 101.6248 101.6248 101.6248 101.6248
291 105.5422 105.5422 105.5422 105.5422 105.5422 105.5422 105.5422 105.5422 105.5422
442 109.0677 109.0677 109.0677 109.0677 109.0677 109.0677 109.0677 109.0677 109.0677
重复同一列。
我该如何解决这个问题?
谢谢!
如果我们需要重置行名称
row.names(indices) <- NULL
另外,我们可以用向量化的方式来做到这一点
100 *indices/indices[1,][col(indices)]