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 7565009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:02:26+00:00 2026-05-30T14:02:26+00:00

If there a function that will give me both counts and column/overall percents in

  • 0

If there a function that will give me both counts and column/overall percents in the same table? I can looked at both tables and reshape2 and don’t see an option for doing it. I’ll give a little example:

data setup

n <- 100
x <- sample(letters[1:3], n, T)
y <- sample(letters[1:3], n, T)
d <- data.frame(x=x, y=y)

With tables

This is very clunky as it requires me to unlist and recombine.

> library(tables)
> (t1 <- tabular(x~y*(n=length), d))

   a  b  c 
 x n  n  n 
 a 13 14 11
 b  8 11 13
 c 10 12  8
> prop.table(matrix(unlist(t1),3,3), 1)
          [,1]      [,2]      [,3]
[1,] 0.3421053 0.3684211 0.2894737
[2,] 0.2500000 0.3437500 0.4062500
[3,] 0.3333333 0.4000000 0.2666667

With Reshape2

This is a little easier, but still not in one.

> library(reshape2)
> (t2 <- acast(d, x~y, length))
Using y as value column: use value_var to override.
   a  b  c
a 13 14 11
b  8 11 13
c 10 12  8
> (t3 <- prop.table(t2,1))
          a         b         c
a 0.3421053 0.3684211 0.2894737
b 0.2500000 0.3437500 0.4062500
c 0.3333333 0.4000000 0.2666667

Desired output

What I really want is output that looks something like this:

> structure(list(
+     a = data.frame(n=t2[,1], pct=t3[,1]),
+     b = data.frame(n=t2[,2], pct=t3[,2]),
+     c = data.frame(n=t2[,3], pct=t3[,3])), 
+   class = 'data.frame',
+   row.names = letters[1:3])
  a.n     a.pct b.n     b.pct c.n     c.pct
a  13 0.3421053  14 0.3684211  11 0.2894737
b   8 0.2500000  11 0.3437500  13 0.4062500
c  10 0.3333333  12 0.4000000   8 0.2666667

Is there a way to do this easily with R?

  • 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-30T14:02:28+00:00Added an answer on May 30, 2026 at 2:02 pm

    Here is one approach, you still need a second step, but it comes before the tabular command so the result is still a tabular object.

    n <- 100 
    x <- sample(letters[1:3], n, T) 
    y <- sample(letters[1:3], n, T) 
    d <- data.frame(x=x, y=y) 
    d$z <- 1/ave( rep(1,n), d$x, FUN=sum )
    
    (t1 <- tabular(x~y*Heading()*z*((n=length) + (p=sum)), d))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a POSIX function that will give me the size of a directory
Is there code in VBA I can wrap a function with that will let
Is there a C API function that will give me the number of CPUs/cores
Is there a win32 function that will tell me if a path is relative
Is there a C library function that will return the index of a character
I know there is a function somewhere that will accept a client rect and
Is there a way to set up a function that will get the user
Occasionally I will know that there is a .NET Framework function that returns a
Is there a built in function in .NET 2.0 that will take two arrays
I'm looking for a hash function that I can use to give uniform unique

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.