Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7066531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:05:35+00:00 2026-05-28T05:05:35+00:00

I can’t find a really intuitive way of doing the most basic thing; creating

  • 0

I can’t find a really intuitive way of doing the most basic thing; creating a summary table with my base variables. The best method I’ve found is currently using tapply:

seed(200)
my_stats <- function(x){
    if (is.factor(x)){
        a <- table(x, useNA="no")
        b <- round(a*100/sum(a),2)

        # If binary
        if (length(a) == 2){
            ret <- paste(a[1], " (", b[1], " %)", sep="")
        }
        return(ret)
    }else{
        ret <- mean(x, na.rm=T)
        if (ret < 1){
            ret <- round(ret, 2)
        }else{
            ret <- round(ret)
        }
        return(ret)
    }
}

library(rms)
groups <- factor(sample(c("Group A","Group B"), size=51, replace=T))
a <- 3:53 
b <- rnorm(51)
c <- factor(sample(c("male","female"), size=51, replace=T))

res <- rbind(a=tapply(a, groups, my_stats),
      b=tapply(b, groups, my_stats),
      c=tapply(c, groups, my_stats))
latex(latexTranslate(res))

The res contains:

> res
  Group A     Group B       
a "28"        "28"          
b "-0.08"     "-0.21"       
c "14 (56 %)" "14 (53.85 %)"

Now this works but it seems very complex and not the most elegant solution. I’ve tried to search for how to create descriptive tables but the all focus on the table(), prop.table(), summary() for just single variable or variables of the same kind.

My question: Is there a package/function that allows an easy way of creating a good-looking latex table? If so, please give a hint of how to get the above result.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:05:36+00:00Added an answer on May 28, 2026 at 5:05 am

    If you rewrite your function so that it always returns a string
    (it sometimes returns a string, sometimes a number, sometimes NULL),
    you can call ddply on the data.frame, without having to specify all the columns.

    f <- function(u) {
      res <- "?" 
      if(is.factor(u) || is.character(u)) {
        u <- table(u, useNA = "no")
        if (length(u) == 0 || sum(u) == 0) { res <- "NA" }
        else { res <- sprintf( "%0.0f%%", 100 * u[1] / sum(u) ) }
      } else {
        u <- mean(u, na.rm=TRUE)
        if(is.na(u)) { res <- "NA" }
        else { res <- sprintf( ifelse( abs(u) < 1, "%0.2f", "%0.0f" ), u ) }
      }
      return( res )
    }
    # Same function, for data.frames
    g <- function(d) do.call( data.frame, lapply(d, f) )
    
    library(plyr)
    ddply(data.frame(a,b,c), .(groups), g)
    

    Since you want LaTeX tables, you may also want to try the following, which does not group the data, but adds sparkline histograms for the numeric variables.

    library(Hmisc)
    latex(describe(d), file="")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can anybody tell me a regular expression to use within some PHP to find
Can't work out a way to make an array of buttons in android. This
Can anyone help me trying to find out why this doesn't work. The brushes
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can't figure out how to do this in a pretty way : I have
Can somebody point me to a resource that explains how to go about having
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can a LINQ enabled app run on a machine that only has the .NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.