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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:58:39+00:00 2026-06-18T10:58:39+00:00

I am trying to reshape my data from this form in exhibit A to

  • 0

I am trying to reshape my data from this form in exhibit A to the form in exhibit B. I’ve tried reshape and looping over the data by each three columns and appending the datasets, but can’t quite get there. How can I reshape this?

A   AFG1    AFG2    AFG3    US1 US2 US3 t
    5   7   9   3   4   5   1980
    6   8   10  4   6   5   2000


B   1   2   3   t   xtry        
    5   7   9   1980    AFG     
    6   8   10  2000    AFG     
    3   4   5   1980    US      
    4   6   5   2000    US      
  • 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-18T10:58:41+00:00Added an answer on June 18, 2026 at 10:58 am

    Assuming your data.frame is called “mydf”, try this:

    reshape(mydf, direction = "long",
            idvar = "t",
            varying = 1:6,
            v.names = c("1", "2", "3"), 
            times = c("AFG", "US"),
            timevar = "xtry")
    #             t xtry 1 2  3
    # 1980.AFG 1980  AFG 5 7  9
    # 2000.AFG 2000  AFG 6 8 10
    # 1980.US  1980   US 3 4  5
    # 2000.US  2000   US 4 6  5
    

    For your problem, you have to be a little more verbose in your arguments than a straightforward reshape problem because your variables are named differently than what R expects (which is in the form of 1.AFG, 2.AFG, and so on).

    For example:

    mydf2 <- mydf
    names(mydf2) <- gsub("([A-Z]+)([0-9]+)", "\\2\\.\\1", names(mydf2))
    

    If your names looked like the following:

    names(mydf2)
    # [1] "1.AFG" "2.AFG" "3.AFG" "1.US"  "2.US"  "3.US"  "t" 
    

    The reshape command is a bit more direct.

    reshape(mydf2, direction = "long", idvar = "t",
            timevar = "xtry", varying = 1:6)
    

    reshape2

    If you are looking for a “reshape2” solution, I actually find it a little more work than using base R’s reshape function (it’s usually the other way around). Here’s what I came up with:

    First, melt the dataset.

    library(reshape2)
    mydf_m <- melt(mydf, id.vars="t")
    head(mydf_m)
    #      t variable value
    # 1 1980     AFG1     5
    # 2 2000     AFG1     6
    # 3 1980     AFG2     7
    # 4 2000     AFG2     8
    # 5 1980     AFG3     9
    # 6 2000     AFG3    10
    

    That “variable” column is pretty useless to us in its current form, so let’s fix it.

    mydf_m <- cbind(mydf_m, 
                    colsplit(gsub("([A-Z]+)([0-9]+)", 
                                  "\\1_\\2", mydf_m$variable), 
                             "_", c("xtry", "var")))
    

    Here’s what the data look like now.

    head(mydf_m)
    #      t variable value xtry var
    # 1 1980     AFG1     5  AFG   1
    # 2 2000     AFG1     6  AFG   1
    # 3 1980     AFG2     7  AFG   2
    # 4 2000     AFG2     8  AFG   2
    # 5 1980     AFG3     9  AFG   3
    # 6 2000     AFG3    10  AFG   3
    

    Now comes the easy part.

    dcast(mydf_m, t + xtry ~ var)
    #      t xtry 1 2  3
    # 1 1980  AFG 5 7  9
    # 2 1980   US 3 4  5
    # 3 2000  AFG 6 8 10
    # 4 2000   US 4 6  5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to plot some data and I am getting this error from
I am trying to plot some categorical data and this answer is very close
I'm trying to convert some data from long to wide using the reshape2 package,
I am trying to reshape/ reduce my data. So far, I employ a for
I have this dataframe DFtest <- data.frame(Zone=rep(c(R1,R2,R3),each=2), Type=rep(c(C1,C2),3), N1=sample(1:6), N2=sample(seq(0,1,length.out=6)), N3=sample(letters[1:6])) DFtest Zone Type
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to figure out how I can do this properly. The print_r looks like
trying to figure out why this is happening - I have an input text
I'm trying to execute this code that processes 70 images and extracts Histogram of
I have 3 txt files with float data. Each file has 17 rows. First

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.