BugNumbers
BugNumbers

Reputation: 3

Extract nested list elements using bracketed numbers and names

After running a repeated measures ANOVA and naming the output

RM_test <- ezANOVA(data=test_data, dv=var_test, wid = .(subject),
within = .(water_year), type = 3)

I looked at the internal structure of the named object using str(RM_test) and received the following:

List of 3
$ ANOVA                        :List of 3
..$ ANOVA                        :'data.frame': 1 obs. of  7 variables:
.. ..$ Effect: chr "water_year"
.. ..$ DFn   : num 2
.. ..$ DFd   : num 22
.. ..$ F     : num 26.8
.. ..$ p     : num 1.26e-06
.. ..$ p<.05 : chr "*"
.. ..$ ges   : num 0.531
..$ Mauchly's Test for Sphericity:'data.frame': 1 obs. of  4 variables:
.. ..$ Effect: chr "water_year"
.. ..$ W     : num 0.875
.. ..$ p     : num 0.512
.. ..$ p<.05 : chr ""
..$ Sphericity Corrections       :'data.frame': 1 obs. of  7 variables:
.. ..$ Effect   : chr "water_year"
.. ..$ GGe      : num 0.889
.. ..$ p[GG]    : num 4.26e-06
.. ..$ p[GG]<.05: chr "*"
.. ..$ HFe      : num 1.05
.. ..$ p[HF]    : num 1.26e-06
.. ..$ p[HF]<.05: chr "*"
$ Mauchly's Test for Sphericity:'data.frame':   1 obs. of  4 variables:
..$ Effect: chr "wtr_yr"
..$ W     : num 0.875
..$ p     : num 0.512
..$ p<.05 : chr ""
$ Sphericity Corrections       :'data.frame':   1 obs. of  7 variables:
..$ Effect   : chr "wtr_yr"
..$ GGe      : num 0.889
..$ p[GG]    : num 4.26e-06
..$ p[GG]<.05: chr "*"
..$ HFe      : num 1.05
..$ p[HF]    : num 1.26e-06
..$ p[HF]<.05: chr "*"

I was able to extract the fourth variable F from the first data frame using RM_test[[1]][[4]][1] but cannot figure out how to extract the third variable p[GG] from the data frame Sphericity Corrections. This data frame appears twice so extracting either one would be fine.

Suggestions on how to do this using bracketed numbers and names would be appreciated.

Upvotes: 0

Views: 187

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73285

The problem seems to be you not knowing how to extract list elements. As you said, there are two Sphericity Corrections data frames, so I will how to get the p[GG] value for both.

using bracketed number

For the first one, we do RM_test[[1]][[3]][[3]]. You can do it step by step to understand it:

x1 <- RM_test[[1]]; str(x1)
x2 <- x1[[3]]; str(x2)
x3 <- x2[[3]]; str(x3)

For the second one, do RM_test[[3]][[3]].

using bracketed name

Instead of using numbers for indexing, we can use names. For the first, do

RM_test[["ANOVA"]][["Sphericity Corrections"]][["p[GG]"]]

For the second, do

RM_test[["Sphericity Corrections"]][["p[GG]"]]

using $

For the first one, do

RM_test$ANOVA$"Sphericity Corrections"$"p[GG]"

For the second one, do

RM_test$"Sphericity Corrections"$"p[GG]"

Note the use of quote "" when necessary.

Upvotes: 2

Related Questions