我有一个列表,其中包含每个列表变量中的多个列。以下数据,
> str(A)
List of 4
$ 1:Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame': 220 obs. of 4 variables:
..$ weight: num [1:220] 42 51 59 64 76 93 106 125 149 171 ...
..$ Time : num [1:220] 0 2 4 6 8 10 12 14 16 18 ...
..$ Chick : Ord.factor w/ 50 levels "18"<"16"<"15"<..: 15 15 15 15 15 15 15 15 15 15 ...
..$ Diet : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
..- attr(*, "formula")=Class 'formula' length 3 weight ~ Time | Chick
.. .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
..- attr(*, "outer")=Class 'formula' length 2 ~Diet
.. .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
..- attr(*, "labels")=List of 2
.. ..$ x: chr "Time"
.. ..$ y: chr "Body weight"
..- attr(*, "units")=List of 2
.. ..$ x: chr "(days)"
.. ..$ y: chr "(gm)"
$ 2:Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame': 120 obs. of 4 variables:
..$ weight: num [1:120] 40 50 62 86 125 163 217 240 275 307 ...
..$ Time : num [1:120] 0 2 4 6 8 10 12 14 16 18 ...
..$ Chick : Ord.factor w/ 50 levels "18"<"16"<"15"<..: 30 30 30 30 30 30 30 30 30 30 ...
..$ Diet : Factor w/ 4 levels "1","2","3","4": 2 2 2 2 2 2 2 2 2 2 ...
..- attr(*, "formula")=Class 'formula' length 3 weight ~ Time | Chick
.. .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
..- attr(*, "outer")=Class 'formula' length 2 ~Diet
.. .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
..- attr(*, "labels")=List of 2
.. ..$ x: chr "Time"
.. ..$ y: chr "Body weight"
..- attr(*, "units")=List of 2
.. ..$ x: chr "(days)"
.. ..$ y: chr "(gm)"
$ 3:Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame': 120 obs. of 4 variables:
..$ weight: num [1:120] 42 53 62 73 85 102 123 138 170 204 ...
..$ Time : num [1:120] 0 2 4 6 8 10 12 14 16 18 ...
..$ Chick : Ord.factor w/ 50 levels "18"<"16"<"15"<..: 34 34 34 34 34 34 34 34 34 34 ...
..$ Diet : Factor w/ 4 levels "1","2","3","4": 3 3 3 3 3 3 3 3 3 3 ...
..- attr(*, "formula")=Class 'formula' length 3 weight ~ Time | Chick
.. .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
..- attr(*, "outer")=Class 'formula' length 2 ~Diet
.. .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
..- attr(*, "labels")=List of 2
.. ..$ x: chr "Time"
.. ..$ y: chr "Body weight"
..- attr(*, "units")=List of 2
.. ..$ x: chr "(days)"
.. ..$ y: chr "(gm)"
$ 4:Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame': 118 obs. of 4 variables:
..$ weight: num [1:118] 42 51 66 85 103 124 155 153 175 184 ...
..$ Time : num [1:118] 0 2 4 6 8 10 12 14 16 18 ...
..$ Chick : Ord.factor w/ 50 levels "18"<"16"<"15"<..: 44 44 44 44 44 44 44 44 44 44 ...
..$ Diet : Factor w/ 4 levels "1","2","3","4": 4 4 4 4 4 4 4 4 4 4 ...
..- attr(*, "formula")=Class 'formula' length 3 weight ~ Time | Chick
.. .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
..- attr(*, "outer")=Class 'formula' length 2 ~Diet
.. .. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
..- attr(*, "labels")=List of 2
.. ..$ x: chr "Time"
.. ..$ y: chr "Body weight"
..- attr(*, "units")=List of 2
.. ..$ x: chr "(days)"
.. ..$ y: chr "(gm)"
我想取每个列表组件中$ weight列的平均值并将其作为向量返回。我试图使用sapply但没有结果。什么是正确的语法?
答案 0 :(得分:1)
您可以直接使用sapply
sapply(A, function(x) mean(x$weight, na.rm=TRUE))
使用可重现的例子
library(nlme)
A <- list(Orthodont, Orthodont)
sapply(A, function(x) mean(x$age))
#[1] 11 11