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

Time:10-17

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.

CodePudding user response:

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))
})
  • Related