unable to merge one table with one empty table

I have a function which is creating a table for subset of data bases one by one. but sometimes one of the table gets empty. so overall table is not generating.

i am looking for an solution what i can update in my current function where if any of the table gets empty it should generate the new column with same rows as "0"

colnamess are the name of subsets coming from another sub function.

t1 <- structure(list(` ` = c("CA", "USA","UK", "GER", "Italy","France", "China"),
                     Local = c("38%", "58%","71%", "78%", "91%", "91%", "84%"),
                     Outsider = c("43%", "28%", "29%"," 9%", " 7%", " 4%", " 5%"), 
                     Mixed = c("19%","13%", " 0%", "13%", "1%", "19%", "11%"),
                     N = c("9999", "9999","9999", "9999", "9999", "9999", "9999")), row.names = c(NA, -7L), class = "data.frame")

t2 <- structure(list(` ` = c("CA", "USA","UK", "GER", "Italy","France", "China"),
                     Local = c("71%", "93%","96%", "96%", "96%", "96%", NA),
                     Outsider = c(" 7%", " 4%", " 4%"," 0%", " 0%", " 0%", NA),
                     Mixed = c(NA,NA,NA,NA,NA,NA,NA),
                     N = c("2800", "2800","2800", "2800", "2800", "2800", NA)), row.names = c(NA, -7L), class = "data.frame")


colnamess <- c("A1","A2")

df1 <- as.data.frame(table(t1$Mixed))

if (nrow(df1) > 0) {
  df1 <- subset(df1[(1:nrow(df1)), ], df1[[2]] > 
                     0)
  names(df1) <- c(" ",colnamess[1])
  
}




df2 <- as.data.frame(table(t2$Mixed))
if (nrow(df2) > 0) {
  df1 <- subset(df2[(1:nrow(df2)), ], df2[[2]] > 
                  0)
  names(df2) <- c(" ",colnamess[1])
  
}

tl <- list(df1,df2)
t2<-Reduce(function(x, y) merge(x, y, all = TRUE,sort = F), tl)
t2 <- flextable::flextable(t2)
t2

whatever if the df1 is blank or df2 , it should generate the table of two column for t2

current output is
image

required output
image

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.