psychTools (version 2.4.3)

df2latex: Convert a data frame, correlation matrix, or factor analysis output to a LaTeX or rtf table


A set of handy helper functions to convert data frames or matrices to LaTeX or rtf tables. Although Sweave is the preferred means of converting R output to LaTeX, it is sometimes useful to go directly from a data.frame or matrix to a LaTeX table. cor2latex will find the correlations and then create a lower (or upper) triangular matrix for latex output. cor2rtf will do the same for rtf output. fa2latex and fa2rtf will create the latex commands for showing the loadings and factor intercorrelations. As the default option, tables are prepared in an approximation of APA format.


df2latex(x,digits=2,rowlabels=TRUE,apa=TRUE,short.names=TRUE,font.size ="scriptsize",
       big.mark=NULL,, heading="A table from the psych package in R",
   caption="df2latex",label="default", char=FALSE, 
cor2latex(x,use = "pairwise", method="pearson", adjust="holm",stars=FALSE,
     font.size ="scriptsize", heading="A correlation table from the psych package in R.",
       cut=0,big=.3,alpha=.05,font.size ="scriptsize",long=FALSE,
       heading="A factor analysis table from the psych package in R",
        big=.3,font.size ="scriptsize", 
        heading="An omega analysis table from the psych package in R",

irt2latex(f,digits=2,rowlabels=TRUE,apa=TRUE,short.names=FALSE, font.size ="scriptsize", heading="An IRT factor analysis table from R", caption="fa2latex",label="default",silent=FALSE,file=NULL,append=FALSE) ICC2latex(icc,digits=2,rowlabels=TRUE,apa=TRUE,ci=TRUE, font.size ="scriptsize",big.mark=NULL,, heading="A table from the psych package in R", caption="ICC2latex",label="default",char=FALSE,silent=FALSE,file=NULL,append=FALSE)

#not all options are yet implemented in these next three functions. df2rtf(x,file=NULL, digits=2,rowlabels=TRUE,width=8.5,old=NULL, apa=TRUE,short.names=TRUE, font.size =10,big.mark=NULL,, heading="A table from the psych package in R", caption="Created with df2rtf",label="default",char=FALSE,stars=FALSE,silent=FALSE, append=FALSE,cut=0,big=.0,abbrev=NULL,long=FALSE)

cor2rtf(x,file=NULL, use = "pairwise", method="pearson", adjust="holm", digits=2, rowlabels=TRUE,width=8.5,lower=TRUE,old=NULL, apa=TRUE,short.names=TRUE, font.size =10,big.mark=NULL,, heading="A correlation matrix from the psych package in R", caption="Created with cor2rtf. left justify output if stars", label="default",char=FALSE,stars=FALSE,silent=FALSE, append=FALSE,cut=0,big=.0,abbrev=NULL,long=FALSE) fa2rtf(f,file=NULL, use = "pairwise", method="pearson", adjust="holm", digits=2, rowlabels=TRUE,width=8.5,lower=TRUE,old=NULL, apa=TRUE,short.names=TRUE, font.size =10,big.mark=NULL,, heading="A Factor analysis from the psych package in R", caption="Created with fa2rtf. ",label="default",char=FALSE,silent=FALSE, append=FALSE,cut=0,big=.0,abbrev=NULL)


A LaTeX table. Note that if showing "stars" for correlations, then one needs to use the siunitx package in LaTex. The entire LaTeX output is also returned invisibly. If using Sweave to create tables, then the silent option should be set to TRUE and the returned object saved as a file. See the last example.

Finally, some users have asked for the ability to convert these output tables into HTML. This may be done using the tth package.

Three functions to write to rtf files (for use in various proprietary word processing languages) have been added with version 2.4.3. These will write to an rtf file and may be formatted directly. df2rtf takes a data frame and writes it as a table with header information.

cor2rtf will take either a data matrix (and find the correlations) or just a correlation matrix. "magic astericks " can be added to the correlations using the stars=TRUE option. In this case, the result table can be left justified in a word processing language to get the numbers to appear correctly justified.

fa2latex and fa2rtf can take the output from either a factor analysis or from fa.lookup.



A data frame or matrix to convert to LaTeX. If non-square, then correlations will be found prior to printing in cor2latex


Round the output to digits of accuracy. NULL for formatting character data


How many characters should be used in column names --defaults to digits + 3


If TRUE, use the row names from the matrix or data.frame


Name the columns with abbreviated rownames to save space


If TRUE formats table in APA style


For factor analyses, should we show the cumulative variance accounted for?


e.g., "scriptsize", "tiny" or anyother acceptable LaTeX font size.


The label appearing at the top of the table


The table caption


in cor2latex, just show the lower triangular matrix


The object returned from a factor analysis using fa or irt.fa.


The label for the table


Comma separate numbers large numbers (big.mark=",")

Do not print NA values


When finding correlations, which method should be used (pearson)


use="pairwise" is the default when finding correlations in cor2latex


If showing probabilities, which adjustment should be used (holm)


Should probability 'magic astericks' be displayed in cor2latex (FALSE)


char=TRUE allows printing tables with character information, but does not allow for putting in commas into numbers


In omega2latex, df2latex and fa2latex, do not print abs(values) < cut


In fa2latex and df2latex boldface those abs(values) > big


If fa has returned confidence intervals, then what values of loadings should be boldfaced?


Either the output of an ICC, or the data to be analyzed.


Should confidence intervals of the ICC be displayed


If TRUE, do not print any output, just return silently -- useful if using Sweave


If specified, write the output to this file


If file is specified, then should we append (append=TRUE) or just write to the file


if TRUE, then do long tables. (requires the longtables package in latex)


When appending output with df2rtf, old is the output from the prior run.


page width in inches for df2rtf


William Revelle with suggestions from Jason French and David Condon and Davide Morselli

See Also

The many LaTeX conversion routines in Hmisc.

To convert these LaTex objects to HTML, you should install the tth package.

Consider the last example for creating HTML


Run this code
        caption="Thurstone Correlation matrix")
df2latex(psych::Thurstone,heading="Thurstone Correlation matrix in APA style")

fa2latex(psych::fa(psych::Thurstone,3),heading="Factor analysis from R in quasi APA style")

#to write to rtf file
#replace the temporary file name with something more useful
fn <- tempfile(pattern="example",fileext=".rtf")  #create a temporary file
#better is to create a local file
# e.g. fn <- "rtf_example.rtf"

cor2rtf(sat.act, file=fn)   #write to the file

dd <- psych::describe(sat.act)
temp <- df2rtf(dd, file=fn, append=TRUE, width=12) #write and keep open
temp1 <-  cor2rtf(sat.act,old=temp,caption=date(), append=TRUE)  #use date as caption 
cor2rtf(sat.act, old=temp1, stars=TRUE) #close the file
#now open this with your word processor and reformat with left justify

#now write a factor analysis output to an output file
# e.g. fn <- "rtf_example.rtf"
f5 <- psych::fa(bfi,5)
temp <- fa2rtf(f5, width=12, file=fn, append=TRUE)  #a normal fa output
fl <- psych::fa.lookup(f5, dictionary=bfi.dictionary)
fa2rtf(fl, old = temp)
##now open this with your word processor

#To convert these latex tables to HTML <- fa2latex(psych::fa(psych::Thurstone,3),
#    heading="Factor analysis from R in quasi APA style")
#library(tth) <- tth(


 #If using Sweave to create a LateX table as a separate file then set silent=TRUE
#LaTex preamble 
#f3 <- fa(Thurstone,3)


Run the code above in your browser using DataLab