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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:16:15+00:00 2026-06-16T09:16:15+00:00

I am trying to write code that will allow me to find the number

  • 0

I am trying to write code that will allow me to find the number of each factor per column in R with the restriction that I want the factor levels to be the same in each column. I thought this should be trivial, but I am running into two places where R does not return quite the value I expect when using apply with factor and using apply with table.

Consider this sample data:

mat <- matrix(sample(1:10,90,replace=TRUE),ncol=10,nrow=9)
mat.levels <- as.character(unique(as.vector(mat)))
mat.factor <- as.data.frame(apply(mat,2,as.character))

My first step was to relevel each column so that the factor levels are the same. At first I tried:

apply(mat.factor,2,factor,levels=mat.levels)
#But the data structure is all wrong, I don't appear to have a factor anymore!
str(apply(mat.factor,2,factor,levels=mat.levels))

So I brute forced it using a loop instead…

for (i in 1:ncol(mat.factor)) {
      levels(mat.factor[,i]) <- mat.levels
    }

Then I ran into another problem with apply. I thought that now I had the factor levels set, if I was missing a given factor in a column the table function should return a count of 0 for that factor level. However, when I used apply it seemed like the factor levels with a zero count were dropped out!

apply(mat.factor,2,table)$V10
str(apply(mat.factor,2,table)$V10)
#But running table just on that one column yields the expected result!
table(mat.factor[,10])
str(table(mat.factor[,10]))

Would somebody explain what is happening in these two cases? What am I misconceptualizing?

  • 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-06-16T09:16:16+00:00Added an answer on June 16, 2026 at 9:16 am

    Read the first sentence in the Details section of ?apply and then run as.matrix(mat.factor) to see the problem. Use lapply for data frames, not apply.

    Here’s an example:

    mat.factor <- as.data.frame(lapply(mat.factor,factor,levels = mat.levels))
    lapply(mat.factor,table)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find some code that, given a string, will allow me to
Hi all I'm trying to write a XML code that will allow me to
I am trying to write some code that will allow the user to draw
I'm trying to write code that will load an image from a resource, and
I am trying to write some code that will generate accurate .proto files from
I am trying to write some code that that will draw the line which
I am currently trying to write some code that will accept some FTP details,
I'm trying to write some Python code that will establish an invisible relay between
Hi I am trying to write an application that will play morse code. I
I'm trying to write code in my controller that when run, will create a

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.