我有两个矩阵:
> 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,但第二个矩阵没有。为什么会发生这种情况以及如何避免它?
答案 0 :(得分:2)
我们可以修改函数以获得带有rownames的输出,即使在某些元素为NA而其他元素不是NA的情况下也是如此。
Fun1 <- function(x) unlist(lapply(list(all = x,
nonnegatives = x[x >= 0.0], negatives = x[x < 0.0]),
function(x) if(length(x)==0) c(median=NA) else c(median=median(x))))
对于第一种情况,第三行的所有元素都是&#39; NA&#39;。
apply(x1, 2, Fun1)
# a c c.1 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。因此,有些人有名字属性,有些人没有名字属性。当我们取消列表时,名称属性将丢失。但是,修改功能以包含&#39; name&#39;即使在NA的情况下,属性也会确保输出具有行名称。
apply(x2, 2, Fun1)
# a c c.1 d e
#all.median 0.01204940 0.006597554 0.01458158 0.008174516 1.212631
#nonnegatives.median 0.08982962 0.051056924 0.11168616 0.068913735 1.212631
#negatives.median -0.04086404 -0.021609462 -0.04586181 -0.027875996 NA
# f
#all.median 2.122302
#nonnegatives.median 2.122302
#negatives.median NA
如果&#39; x1&#39;和&#39; x2&#39;是&#39; data.frames&#39;,这可以通过sapply/lapply
Fun2 <- function(x) c(all.median=median(x), nonnegatives.median =
median(x[x >=0.0]), negatives.median=median(x[x < 0.0]))
sapply(x2, Fun2)
# a c c.1 d e
#all.median 0.01204940 0.006597554 0.01458158 0.008174516 1.212631
#nonnegatives.median 0.08982962 0.051056924 0.11168616 0.068913735 1.212631
#negatives.median -0.04086404 -0.021609462 -0.04586181 -0.027875996 NA
# f
#all.median 2.122302
#nonnegatives.median 2.122302
#negatives.median NA