rreshapepcameltprincomp

PCA in R long form


I would like to do PCA on a dataframe that is in long form:

time1 id1 data11

time1 id2 data12

time2 id1 data21

etc.

Is there an easy way to do this or is the standard way to reshape it and then do princomp. My dataset is pretty large with roughly 40,000 times and 4,000 ids.


Solution

  • For such a simple reshaping I think all you need is

    m <- matrix(mydata[,3],nrow=ntimes,byrow=TRUE)
    princomp(m)
    

    This should give you a ntimes by nIDs matrix to play with. It will be (potentially a lot) faster than reshape.