0

I was wondering if anyone could help me with some R code for extracting useful information from a nested list of dataframes.

The format of the nested dataframes are:

Level1: Five different ways of modelling data (one factor CFA, two factor CFA, ...)

Level2: The model applied to a set of 11 datasets

Level3: Various statistics that are useful for each dataframe

I have fitted the five models on the same eleven datasets.

An example of how this looks can be produced using the following script (sorry if the code is inefficient!)


### Example data that gets transformed
Testdfs<-list()
Testdfs$Summarydata1<-c(1:42)
Testdfs$Summarydata2<-c(43:84)
Testdfs$Summarydata3<-c(85:126)


### Data labels for my project
Datanames<-c("npar", "fmin", "chisq", "df", "pvalue", "baseline.chisq", 
             "baseline.df", "baseline.pvalue", "cfi", "tli", "nnfi", "rfi", 
             "nfi", "pnfi", "ifi", "rni", "logl", "unrestricted.logl", "aic", 
             "bic", "ntotal", "bic2", "rmsea", "rmsea.ci.lower", "rmsea.ci.upper", 
             "rmsea.pvalue", "rmr", "rmr_nomean", "srmr", "srmr_bentler", 
             "srmr_bentler_nomean", "crmr", "crmr_nomean", "srmr_mplus", "srmr_mplus_nomean", 
             "cn_05", "cn_01", "gfi", "agfi", "pgfi", "mfi", "ecvi")

names(Testdfs$Summarydata1)<-Datanames
names(Testdfs$Summarydata2)<-Datanames
names(Testdfs$Summarydata3)<-Datanames

### Creating the nested list from the three initial sets of example data
CombinedTest<-list()
Testfunction1<-function(X){X+1}
CombinedTest$OneFactor<-lapply(Testdfs,Testfunction1)

Testfunction2<-function(X){X+2}
CombinedTest$TwoFactor<-lapply(Testdfs,Testfunction2)

Testfunction3<-function(X){X+3}
CombinedTest$AlternativeTwoFactor<-lapply(Testdfs,Testfunction3)

Testfunction4<-function(X){X+4}
CombinedTest$TwoFactorOrthogonal<-lapply(Testdfs,Testfunction4)

Testfunction5<-function(X){X+5}
CombinedTest$SecondOrder<-lapply(Testdfs,Testfunction5)

remove(Testdfs,Datanames)
##Nested list contains 5 different "models" applied to the 3 dataframes (I have 11 in my actual database)

This produces a series of fit statistics for that dataset in that model i.e.

CombinedTest[["OneFactor"]][["Summarydata1"]]
               npar                fmin               chisq                  df              pvalue      baseline.chisq         baseline.df 
                  2                   3                   4                   5                   6                   7                   8 
    baseline.pvalue                 cfi                 tli                nnfi                 rfi                 nfi                pnfi 
                  9                  10                  11                  12                  13                  14                  15 
                ifi                 rni                logl   unrestricted.logl                 aic                 bic              ntotal 
                 16                  17                  18                  19                  20                  21                  22 
               bic2               rmsea      rmsea.ci.lower      rmsea.ci.upper        rmsea.pvalue                 rmr          rmr_nomean 
                 23                  24                  25                  26                  27                  28                  29 
               srmr        srmr_bentler srmr_bentler_nomean                crmr         crmr_nomean          srmr_mplus   srmr_mplus_nomean 
                 30                  31                  32                  33                  34                  35                  36 
              cn_05               cn_01                 gfi                agfi                pgfi                 mfi                ecvi 
                 37                  38                  39                  40                  41                  42                  43 

What I would like to do, is create a table/dataframe for each of the initial dataframes (Summarydata1, summarydata2, summarydata3) that shows the fit statistics down the rows, and the five models that the dataframe has been applied to as columns. Please find an example below (without made up data).

enter image description here

Does anyone know how I could accomplish this? I'm new to R and know that using lists is important, but I find the logic around manipulating them difficult.

Thanks.

  • Hello and welcome to SO! We would love to help but you'd need to help us help you by providing a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Desmond Nov 11 '21 at 02:02
  • Hi @Desmond Thanks for the welcome. I have added a script to generate example data to allow this to be reproducible. I hope this is sufficient. – Josh Blake Nov 11 '21 at 12:31

0 Answers0