使用apply()

时间:2018-08-26 01:49:11

标签: r list lapply paste

我正在尝试使用lapply运行带有一堆输出的线性模型。我很难分辨哪个输出属于哪个参数对,所以我想在每个输出中粘贴列表名称。这里是一些数据:

Site    Date    Season  Species BodySize    Mass    SeasonGDD
1   3/19/17 winter  infrequens  6   1.134965577 3.556416667
1   3/19/17 winter  infrequens  6   1.134965577 3.556416667
1   3/19/17 winter  infrequens  6   1.134965577 3.556416667
1   3/19/17 winter  infrequens  6   1.134965577 3.556416667
1   3/19/17 winter  infrequens  6   1.134965577 3.556416667
1   3/19/17 winter  infrequens  6.2 1.245407892 3.556416667
1   3/19/17 winter  infrequens  6.2 1.245407892 3.556416667
1   3/19/17 winter  infrequens  6.2 1.245407892 3.556416667
1   3/19/17 winter  infrequens  6.4 1.362573803 3.556416667
1   3/19/17 winter  infrequens  6.4 1.362573803 3.556416667
1   3/19/17 winter  doddsii 6.4 1.362573803 3.556416667
1   3/19/17 winter  infrequens  6.4 1.362573803 3.556416667
1   3/19/17 winter  infrequens  6.4 1.362573803 3.556416667
4   3/19/17 winter  infrequens  6.4 1.362573803 3.556416667
4   3/19/17 winter  doddsii 6.6 1.486643287 3.556416667
3   3/19/17 winter  infrequens  6.6 1.486643287 3.556416667
2   3/19/17 winter  infrequens  6.6 1.486643287 3.556416667
2   3/19/17 winter  infrequens  6.6 1.486643287 3.556416667
1   3/19/17 winter  infrequens  6.6 1.486643287 3.556416667
1   3/19/17 winter  infrequens  6.6 1.486643287 3.556416667
1   3/19/17 winter  doddsii 6.6 1.486643287 3.556416667
1   3/19/17 winter  doddsii 6.6 1.486643287 3.556416667
1   3/19/17 winter  cockerelli  6.8 1.293245066 3.556416667
5   3/19/17 winter  infrequens  6.8 1.617795375 3.556416667
5   3/19/17 winter  infrequens  6.8 1.617795375 3.556416667
3   3/19/17 winter  infrequens  6.8 1.617795375 3.556416667
1   3/19/17 winter  infrequens  6.8 1.617795375 3.556416667
5   3/19/17 winter  doddsii     6.8 1.617795375 3.556416667
4   3/19/17 winter  grandis     6.9 1.347971145 3.556416667
1   3/19/17 winter  doddsii     7   1.75620819  3.556416667
1   3/19/17 winter  infrequens  7   1.75620819  3.556416667
1   3/19/17 winter  infrequens  7   1.75620819  3.556416667
1   3/19/17 winter  infrequens  7   1.75620819  3.556416667
1   3/19/17 winter  doddsii     7.2 1.902058976 3.556416667
1   3/19/17 winter  infrequens  7.2 1.902058976 3.556416667
2   3/19/17 winter  infrequens  7.2 1.902058976 3.556416667
2   3/19/17 winter  doddsii     7.4 2.055524128 3.556416667
2   3/19/17 winter  doddsii     7.4 2.055524128 3.556416667
2   3/19/17 winter  doddsii     7.4 2.055524128 3.556416667
1   3/19/17 winter  doddsii     7.4 2.055524128 3.556416667
1   3/19/17 winter  doddsii     7.4 2.055524128 3.556416667
1   3/19/17 winter  cockerelli  7.5 1.707996411 3.556416667
1   3/19/17 winter  doddsii     7.6 2.216779222 3.556416667
3   3/19/17 winter  doddsii     7.6 2.216779222 3.556416667
4   3/19/17 winter  doddsii     7.6 2.216779222 3.556416667
1   3/19/17 winter  infrequens  7.6 2.216779222 3.556416667
1   3/19/17 winter  cockerelli  7.7 1.840497347 3.556416667
1   3/19/17 winter  doddsii 7.8 2.385999034 3.556416667
2   3/19/17 winter  doddsii 7.8 2.385999034 3.556416667
2   3/19/17 winter  doddsii 7.8 2.385999034 3.556416667
3   3/19/17 winter  doddsii 8.2 2.749028097 3.556416667
1   3/19/17 winter  doddsii 8.6 3.145994526 3.556416667
5   3/19/17 winter  cockerelli  8.7 2.603054046 3.556416667
1   3/19/17 winter  doddsii 9   3.578270231 3.556416667
5   3/19/17 winter  doddsii 9.4 4.047216436 3.556416667
5   3/19/17 winter  cockerelli  9.7 3.545136654 3.556416667
5   3/19/17 winter  cockerelli  10.6    4.560698666 3.556416667
4   3/19/17 winter  cockerelli  10.8    4.80925792  3.556416667
4   3/19/17 winter  grandis 10.8    4.80925792  3.556416667
4   3/19/17 winter  cockerelli  10.9    4.936758182 3.556416667
1   3/19/17 winter  cockerelli  11  5.066427825 3.556416667
2   3/19/17 winter  cockerelli  11  5.066427825 3.556416667
1   3/19/17 winter  grandis 11  5.066427825 3.556416667
1   3/19/17 winter  cockerelli  11.2    5.332341968 3.556416667
2   3/19/17 winter  cockerelli  11.5    5.747899893 3.556416667

这是我的代码:

newdata = split(data, list(data$Species, data$Site))

 seasonCompare = lapply(newdata, function(x) {
   m.interaction = (lm(BodySize ~ SeasonGDD*Site, data = x))
   #Anova table
   anova(m.interaction)
   # Obtain slopes
   m.interaction$coefficients
   m.lst = lstrends(m.interaction, "Site", var="SeasonGDD")
   # Compare slopes
   print(pairs(m.lst))
   })

这是我希望输出看起来像的样子:

[[1]]$cockerelli.1
P value adjustment: tukey method for comparing a family of 5 estimates 
 contrast      estimate           SE  df t.ratio p.value
 1 - 2     0.0044905951 0.0020291812 525   2.213  0.1764
 1 - 3     0.0007809249 0.0004559391 525   1.713  0.4269
 1 - 4     0.0022220434 0.0004862632 525   4.570  0.0001
 1 - 5     0.0610176389 0.0141858874 525   4.301  0.0002
 2 - 3    -0.0037096702 0.0020046493 525  -1.851  0.3457

[[1]]$infrequens.1
P value adjustment: tukey method for comparing a family of 5 estimates 
 contrast      estimate           SE   df t.ratio p.value
 1 - 2    -4.003309e-04 0.0002637222 3067  -1.518  0.5508
 1 - 3    -2.272248e-04 0.0001984400 3067  -1.145  0.7825
 1 - 4    -3.664736e-04 0.0001983656 3067  -1.847  0.3464

非常感谢!

不需要的输出:

contrast     estimate           SE  df t.ratio p.value
 1 - 2    -0.010726867 0.0014693633 345  -7.300  <.0001
 1 - 3    -0.008197681 0.0009939915 345  -8.247  <.0001
 1 - 4    -0.009577345 0.0010252581 345  -9.341  <.0001
 1 - 5    -0.424121019 0.0759824627 345  -5.582  <.0001
 2 - 3     0.002529186 0.0013368246 345   1.892  0.3236
 2 - 4     0.001149522 0.0013602334 345   0.845  0.9163
 2 - 5    -0.413394152 0.0759877208 345  -5.440  <.0001
 3 - 4    -0.001379664 0.0008241514 345  -1.674  0.4514
 3 - 5    -0.415923338 0.0759800152 345  -5.474  <.0001
 4 - 5    -0.414543674 0.0759804306 345  -5.456  <.0001

 P value adjustment: tukey method for comparing a family of 5 estimates  
 contrast      estimate           SE  df t.ratio p.value
 1 - 2    -3.445648e-06 0.0008580996 279  -0.004  1.0000
 1 - 3     3.055790e-03 0.0011595803 279   2.635  0.0240
 2 - 3     3.059235e-03 0.0012805524 279   2.389  0.0461

2 个答案:

答案 0 :(得分:0)

请勿使用lapply,而使用sapply-它会保留列名

请查看sapply输出的效果如何:

> df <- data.frame(one=c(1,1), foo=c(4,5))

> lapply(df, mean)
$one
[1] 1

$foo
[1] 4.5

> sapply(df, mean)
 one fuzz 
 1.0  4.5

答案 1 :(得分:-1)

尝试:

names(seasoneCompare) <- names(newdata)