Exporting PCA data to CSV

604 views
Skip to first unread message

Colin Illes

unread,
Aug 20, 2021, 1:01:22 PM8/20/21
to geomorph R package
Hello, 

How do I export PC1 and PC2 data from a PCA into a CSV file or equivalent?

Thanks, 

Colin 

Bryan H. Juarez

unread,
Aug 20, 2021, 1:07:23 PM8/20/21
to geomorph-...@googlegroups.com
Colin, 

One can use the write.csv function to do this. The input would depend on how you performed PCA. If gm.prcomp, the PC scores are stored in $x. 

Hope that answers your question.

Best, 
Bryan

--
You received this message because you are subscribed to the Google Groups "geomorph R package" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geomorph-r-pack...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/5c38a129-d844-4e55-af7b-93f887805736n%40googlegroups.com.


--
Bryan H. Juarez, PhD 
He/Him
NSF Postdoctoral Scholar
O'Connell and Hadly Labs
Biology Dept.
Stanford University
Twitter: @bhjuarez


Colin Illes

unread,
Aug 20, 2021, 1:20:42 PM8/20/21
to geomorph-...@googlegroups.com
Thanks Bryan,

I used PCA<-gm.prcomp(fishes.gpa$coords)

I still cannot get the correct output for the CSV. 100 % user error. Any further help would be appreciated.

Colin

Sent from my iPad

On Aug 20, 2021, at 1:07 PM, Bryan H. Juarez <bryanh...@gmail.com> wrote:


You received this message because you are subscribed to a topic in the Google Groups "geomorph R package" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/geomorph-r-package/xjwIVUbC7o4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to geomorph-r-pack...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/geomorph-r-package/CAJgz5LxdRwJVqEMaPkytrQ5Aak6YqopyjmA732cE%3DDHgDTP4MQ%40mail.gmail.com.

Bryan H. Juarez

unread,
Aug 20, 2021, 1:41:20 PM8/20/21
to geomorph-...@googlegroups.com
The "PCA" object will have many things within it, as is the case with the outputs of many functions in R. See the help file: ?gm.prcomp
Your object "PCA" contains the scores, a rotation matrix, etc. To extract only the PC scores, you must use PCA$x.

PCA$x #PC scores are stored in $x within PCA
write.csv(PCA$x, "anyfilename.csv") #saves PCA$x to a csv file, but up to user to customize output using options within write.csv as necessary.

Bryan

Reply all
Reply to author
Forward
0 new messages