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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:56:23+00:00 2026-05-28T03:56:23+00:00

I have a large dataframe and want to tabulate all variable-paris. table() and xtabs()

  • 0

I have a large dataframe and want to tabulate all variable-paris. table() and xtabs() both do this, but the problem is:

  1. xtabs() allows me to drop unused variable levels, which I need, but doesn’t let
    me define the names of the dimensions
  2. table() allows me to define dimension names, but not to drop the unused levels.

The reason I need to define the dimensionnames is that all this happens inside a for-loop (becasue I need to do ‘everybody by everybody’), and this renders the names meaningless. Below is a ‘simple’ example to show what I mean.

var.3=factor(rep(c("m","f","t"), c(5,5,2)))
df <- data.frame(var.1=rep(1:4, 1:4), var.2=rep(c("A","B"), 5), var3=var.3[1:10])
levels(df[,3])           # the "t" level is not in the df!
tabs.list<- list()
xtabs.list<- list()
for (i in 1:(ncol(df)-1)){
  for (j in (i+1):ncol(df)) {
    tabs.list[[paste(sep=" ", colnames(df)[i], "by",colnames(df)[j])]] <-
      table(df[,i],df[,j], dnn=list(colnames(df)[i], colnames(df)[j]))
    xtabs.list[[paste(sep=" ", colnames(df)[i], "by",colnames(df)[j])]] <-
      xtabs(~df[,i]+df[,j], drop.unused.levels=TRUE)
  }
}
tabs.list
xtabs.list
#What I want: 
for (i in 1:length(xtabs.list)){
names(dimnames(xtabs.list[[i]])) <- names(dimnames(tabs.list[[i]]))
}
xtabs.list

So two functions for crossclassifying data each have an option I would like to use!? Why can’t I do both?

  • 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-28T03:56:24+00:00Added an answer on May 28, 2026 at 3:56 am

    It’s pretty easy to “de-factorize” arguments by wrapping in as.character

    tabs.list<- list()
    for (i in 1:(ncol(df)-1)){
        for (j in (i+1):ncol(df)) {
          tabs.list[[paste(sep=" ", colnames(df)[i], "by",colnames(df)[j])]] <-
            table( as.character(df[,i]), 
                   as.character(df[,j]), 
                   dnn=list(colnames(df)[i], colnames(df)[j])) 
                                  }
                               }
    tabs.list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to split characters. Although I have a large dataframe to work, the
I have a input dataframe like this (the real one is very large, so
I have large String variable named text . I want to be able to
I have very large dataframe and I need to choose variable number satisfying certain
I have large database table, approximately 5GB, now I wan to getCurrentSnapshot of Database
I have toyed with a number of ideas to do this, but so far
I have large zoomable bitmaps with text, that I want to resize a little
I have two tables: Dictionary, Names. Both tables have large number of rows (like
So I have large blocks of text in my website, but right now the
I have a large dataframe (~ 600K rows) with a string-value column (link) doc_id,link

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.