2

I'm using lapply to loop through a list of dataframes and apply the same set of functions. This works fine when lapply has just one function, but I'm struggling to see how I store/print the output from multiple functions - in that case, I seem to only get output from one 'loop'.

So this:

output <- lapply(dflis,function(lismember) vss(ISEQData,n=9,rotate="oblimin",diagonal=F,fm="ml"))

works, while the following doesn't:

output <- lapply(dflis,function(lismember){
outputvss <- vss(lismember,n=9,rotate="oblimin",diagonal=F,fm="ml")
nefa <- (EFA.Comp.Data(Data=lismember, F.Max=9, Graph=T))
})

I think this dummy example is an analogue, so in other words:

nbs <- list(1==1,2==2,3==3,4==4)
nbsout <- lapply(nbs,function(x) length(x))

Gives me something I can access, while I can't see how to store output using the below (e.g. the attempt to use nbsout[[x]][2]):

nbs <- list(1==1,2==2,3==3,4==4)
nbsout <- lapply(nbs,function(x){
  nbsout[[x]][1]<-typeof(x)
  nbsout[[x]][2]<-length(x)
  }
                 )

I'm using RStudio and will then be printing outputs/knitting html (where it makes sense to display the results from each dataset together, rather than each function-output for each dataset sequentially).

2 Answers 2

3

You should return a structure that include all your outputs. Better to return a named list. You can also return a data.frame if your outputs have all the same dimensions.

otutput <- lapply(dflis,function(lismember){
    outputvss <- vss(lismember,n=9,rotate="oblimin",diagonal=F,fm="ml")
    nefa <- (EFA.Comp.Data(Data=lismember, F.Max=9, Graph=T))

    list(outputvss=outputvss,nefa=nefa)  
    ## or data.frame(outputvss=outputvss,nefa=nefa) 
})

When you return a data.frame you can use sapply that simply outputs the final result to a big data.frame. Or you can use the classical:

do.call(rbind,output)

to aggregate the result.

Sign up to request clarification or add additional context in comments.

Comments

2

A function should always have an explicit return value, e.g.

output <- lapply(dflis,function(lismember){
   outputvss <- vss(lismember,n=9,rotate="oblimin",diagonal=F,fm="ml")
   nefa <- (EFA.Comp.Data(Data=lismember, F.Max=9, Graph=T))
   #return value:
   list(outputvss, nefa)
})

output is then a list of lists.

6 Comments

I guess it will be easier to access the values if they would use c instead of list (at least for their reproducible example, when the result is just one value per function)
@DavidArenburg c is just a shortcut for a list, isn't?
@agstudy, the output is a vector per each argument within the list, instead of list per each argument within the list. Try lapply(nbs, function(x){ nbsout1<-typeof(x) ; nbsout2<-length(x); c(nbsout1, nbsout2)})
@DavidArenburg but you assume that the 2 has the same dimensions , indeed c will unlist the result to get the same vector ....
@DavidArenburg Maybe. This is just an example. I have no idea what kind of objects outputvss and nefa are.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.