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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:44:13+00:00 2026-06-04T11:44:13+00:00

I have a data frame that looks like this, with two key columns and

  • 0

I have a data frame that looks like this, with two key columns and then a count of things that come in three different types.

  Year Month Urban Suburban Rural
1    1     1    11       12    13
2    1     2    21       22    23

I want to expand each row so that it lists the type as a factor and then the number in that type, so something like this:

  Year Month     Type Number
1    1     1    Urban     11
2    1     1 Suburban     12
3    1     1    Rural     13
4    1     2    Urban     21
5    1     2 Suburban     22
6    1     2    Rural     23

is there a function that does this painlessly?

  • 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-04T11:44:14+00:00Added an answer on June 4, 2026 at 11:44 am

    This is precisely what the reshape and reshape2 packages are designed to do:

    require(reshape2)
    x <- read.table(text = "Year Month Urban Suburban Rural
    1    1     1    11       12    13
    2    1     2    21       22    23")
    
    #Specify the variables that are your ID variables. The others will form your "long" data
    x.m <- melt(x, id.vars = c("Year", "Month"))
    #-----  
    Year Month variable value
    1    1     1    Urban    11
    2    1     2    Urban    21
    3    1     1 Suburban    12
    ...
    

    There is a paper in the journal of statistical software that’s a great place to get started.

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

Sidebar

Related Questions

I have a data.frame that looks like this: > head(ff.df) .id pio caremgmt prev
I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,
I have a data.frame that looks like this > head(df) Memory Memory Memory Memory
I have a data frame that looks like this: user1,product1,0 user1,product2,2 user1,product3,1 user1,product4,2 user2,product3,0
I have an R data frame that looks like this: z = as.data.frame(list(Col1=c(a,c,e,g),Col2=c(b,d,f,h),Col3=c(1,2,5,3,5,7,9,8,1))) >
I have a large data frame (named z ) that looks like this: RPos
I have a semi-melted data frame that looks like this: head(final_melt) Group Source variable
I have a data.frame that looks like this. x a 1 x b 2
I have a R data frame that looks like this: Country Property Value Canada

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.