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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:22:41+00:00 2026-05-30T08:22:41+00:00

I am using a subset to extract from set of a column values I

  • 0

I am using a subset to extract from set of a column values

I have a DF with several columns, one of them is state. I need to get frequencies for state but just for some set of states. So I have this:

tmp <- subset(DF, DF$STATE %in% SOMESTATES)
a   <- as.data.frame(table(tmp$STATE))

This is almost fine. The tmp data frame has only the records belonging to the SOMESTATES set, fine.

The issue is a. The table result is the complete frequencies for the whole DF, not the tmp only. The other has zero values.

My problem here is some states in SOMESTATES has zero appearances in the DF, which this prevents me to use droplevels. droplevels takes out also those zero values. Here as well:

If I use droplevels like this, for example, I loose the zero values for some states that I need.

tmp <- subset(DF, DF$STATE %in% SOMESTATES)
tmp2 <- droplevels(tmp)
table(tmp2$STATE)

presents all the states not only the ones in SOMESTATES

Any advise is appreciated

  • 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-30T08:22:42+00:00Added an answer on May 30, 2026 at 8:22 am

    Try something like:

    tmp <- subset(DF, DF$STATE %in% SOMESTATES) 
    tmp$STATE <- factor(tmp$STATE, levels=SOMESTATES) 
    table(tmp$STATE) 
    

    The factor function will recreate it into a new factor whose levels match SOMESTATES exactly. Those states who are not in SOMESTATES will not be included as they are neither in the data nor SOMESTATES, but those states with 0 count that are in SOMESTATES will still be included in the factor, and hence the table with 0 counts.

    Note also that the factor levels will be ordered by SOMESTATES so the order in that vector will be the order that shows up in the tables or plots using it. Generally this is benign or useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using PHP, how do I get an entire subset of nodes from an XML
I have requirement of selecting s subset of data from a collection using a
I'm trying setup a subset of boost and get it properly compiled using bjam,
I have to extract sections of an array and set the chunk to another
I have a small subset of fields that need blanked on a page (so
Using the following two R vectors, I want to extract a subset of valMe
I have a matrix X1 with 6 columns. Column 3 in this X1 matrix
I wrote a small internal web app using (a subset of) pylons . As
Using Python how do you reduce a list of lists by an ordered subset
I've been considering using Haskell's Parsec parsing library to parse a subset of Java

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.