Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to apply the function `flextable` on a list of tables?

I have this list L of tables ( I used df1 and df2 to produce the tables t1 and t2 but in my real case we have just tables to work on them ) :


df1 = data.frame(male = c(0,1,0,0,1,1), female = c(0,0,0,1,1,0))

df2 = data.frame(adult = c(1,1,0,0), teenager = c(1,1,1,1) )

t1 = table(df1)

t2 = table(df2)

L = list(t1,t2)

And I wish to apply the function flextable to produce a table for each table of this list L (meaning I need to convert each table into a dataframe and then apply lapply to make it work. However I wasn’t able to do it the right way.)

appreciate the help and thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use lapply() to apply a function to a list. Since flextable() only works on dataframes, you need to convert the table to a dataframe first:

lapply(L, function(x) {
  flextable::flextable(as.data.frame(x))
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading