【发布时间】:2015-03-21 07:30:49
【问题描述】:
我有两个矩阵:
> x1
a c c d e f
2007-12 0.1988856 0.11641236 0.2807305 0.14357490 1.4118684 2.411518
2008-12 0.4176413 0.14818832 0.2296860 0.19274035 0.5882893 1.549960
2009-12 0.1838148 0.08337788 0.1525942 0.10716483 1.0585924 1.830152
2010-12 0.1234445 0.05693444 0.1056719 0.07402619 1.1792450 1.856027
2011-12 0.3646326 0.15624401 0.2733914 0.22585432 1.3659229 1.749772
2012-12 0.2238182 0.09063396 0.1523118 0.17185438 2.2483446 1.680515
2013-12 0.1845394 0.08367327 0.1530842 0.11662962 1.1007866 1.829547
> x2
a c c d e f
2007-12 -0.01857119 -0.004988447 -0.006820523 -0.005919000 0.3110451 1.367264
2008-12 0.35842665 0.098544182 0.135910903 0.117976957 0.6160532 1.379187
2009-12 -0.04086404 -0.021609462 -0.045861809 -0.027875996 1.1854275 2.122302
2010-12 0.06818902 0.038316040 0.087461421 0.049706569 1.2126310 2.282632
2011-12 0.11147022 0.063797807 0.149175481 0.088120901 1.3743242 2.338254
2012-12 0.01204940 0.006597554 0.014581585 0.008174516 1.4905291 2.210150
2013-12 -0.15578076 -0.070454357 -0.128628802 -0.102376037 1.4801237 1.825704
接下来我使用以下语句应用以下函数:
Fun <- function(x) unlist(lapply(list(all = x, nonnegatives = x[x >= 0.0], negatives = x[x < 0.0]), function(x) c(median = median(x))))
apply(x1, 2, Fun)
apply(x2, 2, Fun)
...并得到以下结果:
> apply(x1, 2, Fun)
a c c d e f
all.median 0.1988856 0.09063396 0.1530842 0.1435749 1.179245 1.829547
nonnegatives.median 0.1988856 0.09063396 0.1530842 0.1435749 1.179245 1.829547
negatives.median.NA NA NA NA NA NA NA
> apply(x2, 2, Fun)
a c c d e f
[1,] 0.01204940 0.006597554 0.01458159 0.008174516 1.212631 2.122302
[2,] 0.08982962 0.051056923 0.11168616 0.068913735 1.212631 2.122302
[3,] -0.04086404 -0.021609462 -0.04586181 -0.027875996 NA NA
在那里看到,第一个矩阵有 row.names,但第二个矩阵 - 没有。为什么会发生这种情况以及如何避免?
【问题讨论】:
-
我不知道你想要的输出是什么样的:你现在只有 3 行,你希望这个 row.names 出现在哪里?
-
操作,抱歉,我没听懂这个问题。您正在比较两个输出矩阵