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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:55:30+00:00 2026-06-08T20:55:30+00:00

Suppose that we have a data frame that looks like set.seed(7302012) county <- rep(letters[1:4],

  • 0

Suppose that we have a data frame that looks like

set.seed(7302012)

county         <- rep(letters[1:4], each=2)
state          <- rep(LETTERS[1], times=8)
industry       <- rep(c("construction", "manufacturing"), 4)
employment     <- round(rnorm(8, 100, 50), 0)
establishments <- round(rnorm(8, 20, 5), 0)

data <- data.frame(state, county, industry, employment, establishments)

  state county      industry employment establishments
1     A      a  construction        146             19
2     A      a manufacturing        110             20
3     A      b  construction        121             10
4     A      b manufacturing         90             27
5     A      c  construction        197             18
6     A      c manufacturing         73             29
7     A      d  construction         98             30
8     A      d manufacturing        102             19

We’d like to reshape this so that each row represents a (state and) county, rather than a county-industry, with columns construction.employment, construction.establishments, and analogous versions for manufacturing. What is an efficient way to do this?

One way is to subset

construction <- data[data$industry == "construction", ]
names(construction)[4:5] <- c("construction.employment", "construction.establishments")

And similarly for manufacturing, then do a merge. This isn’t so bad if there are only two industries, but imagine that there are 14; this process would become tedious (though made less so by using a for loop over the levels of industry).

Any other ideas?

  • 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-08T20:55:32+00:00Added an answer on June 8, 2026 at 8:55 pm

    This can be done in base R reshape, if I understand your question correctly:

    reshape(data, direction="wide", idvar=c("state", "county"), timevar="industry")
    #   state county employment.construction establishments.construction
    # 1     A      a                     146                          19
    # 3     A      b                     121                          10
    # 5     A      c                     197                          18
    # 7     A      d                      98                          30
    #   employment.manufacturing establishments.manufacturing
    # 1                      110                           20
    # 3                       90                           27
    # 5                       73                           29
    # 7                      102                           19 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a data frame like this: df <- data.frame(id = rep(c(1001:1004), each
Suppose I have a data frame, df, that looks like: f t1 t2 t3
Suppose, you have a data.frame like this: x <- data.frame(v1=1:20,v2=1:20,v3=1:20,v4=letters[1:20]) How would you select
Suppose that you have a big Data Entry Web Application Like Microsoft CRM ,
Suppose that you have a data frame with many rows and many columns. The
Suppose I have a data.frame that's completely numeric . If I make one entry
I have data that more or less looks like this (don't know how to
Suppose I have a data frame in R that has names of students in
Suppose I have an R data frame with columns that specify location (lat/long), height,
Suppose I have an R dataframe that looks like this, where end.group signifies the

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.