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

  • SEARCH
  • Home
  • 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 6871365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:47:49+00:00 2026-05-27T03:47:49+00:00

I have a 2D contingency table in R ; it is a table object.

  • 0

I have a 2D contingency table in R; it is a table object. I want to transform it into a new table by applying a function on each of its elements.
I looked at sapply, tapply, etc., but they are all aimed at summarising/aggregating the data. I’ve written my own mapping function which does this, which I reproduce below:

map.table = function(t,fn)
{
    rows = dim(t)[1]
    columns = dim(t)[2]
    x = matrix(nrow=rows, ncol=columns)

    rownames(x) = unlist(dimnames(bush.t)[1], use.names=FALSE)
    colnames(x) = unlist(dimnames(bush.t)[2], use.names=FALSE)
    for(row in seq(from=1, to=rows))
    {
        for(column in seq(from=1, to=columns))
        {
            x[row,column] = fn(t[row,column])
        }
    }
    as.table(x)
}

This creates a matrix from scratch, fills up the dimension names, and the elements. Is there a better way of doing this? Is there an R function/package which already does this functionality?

  • 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-27T03:47:50+00:00Added an answer on May 27, 2026 at 3:47 am

    You can probably just use apply:

    set.seed(21)
    x <- data.frame(a=sample(letters[1:5],20,TRUE),
                    b=sample(letters[1:5],20,TRUE))
    y <- table(x)
    z <- as.table(apply(y, 1:2, sqrt))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If i want to calculate the amount of k-dimensional contingency tables which formula should
Have converted devise new session from erb to Haml but doens't work, this is
Have a photography site that I want to prevent image copying from. How can
Have have this line of code in my form when I create a new
Have run into a bug with glibc's malloc(): http://sourceware.org/bugzilla/show_bug.cgi?id=4349 and am thinking a work
have an an array String classname[]={'a','b','c','d'}; ArrayAdapter<CharSequence> adapterClasses = new ArrayAdapter<CharSequence>( getApplicationContext(), R.layout.spinner_item_class, R.id.spinnerclasstxt,
I have a function that, for argument sake, has two lines of code. Line
Have just started to get into CakePHP since a couple of weeks back. I
Have finally got a responsive site working (of a fashion). What I want to
Have some code: using (var ctx = new testDataContext()) { var options = new

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.