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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:49:46+00:00 2026-06-18T07:49:46+00:00

I would like to reshape a data.frame that looks like this: permno dte ttm

  • 0

I would like to reshape a data.frame that looks like this:

     permno         dte ttm var1 var2 var3
1    123  2012-01-01  20    1   10  100
2    123  2012-01-01  30   -1   10  100
3    124  2012-01-01  20    2   20  200
4    124  2012-01-01  30   -2   20  200

I would like to make my data.frame look the following way:

  permno         dte var1_20 var1_30 var2 var3
1    123  2012-01-01       1      -1   10  100
2    124  2012-01-01       2      -2   20  200

I have been attempting to do this with reshape2 package but I am unable to isolate var1 from the rest and keep getting var2_20 and var2_30 for example in the results. Does anyone know how to do this using the reshape2 package?

data.frame dput:

> dput(DF)
structure(list(permno = c(123L, 123L, 124L, 124L), dte = structure(c(1L, 
1L, 1L, 1L), .Label = " 2012-01-01", class = "factor"), ttm = c(20L, 
30L, 20L, 30L), var1 = c(1L, -1L, 2L, -2L), var2 = c(10L, 10L, 
20L, 20L), var3 = c(100L, 100L, 200L, 200L)), .Names = c("permno", 
"dte", "ttm", "var1", "var2", "var3"), class = "data.frame", row.names = c(NA, 
-4L))
> dput(result)
structure(list(permno = 123:124, dte = structure(c(1L, 1L), .Label = " 2012-01-01", class = "factor"), 
    var1_20 = 1:2, var1_30 = c(-1L, -2L), var2 = c(10L, 20L), 
    var3 = c(100L, 200L)), .Names = c("permno", "dte", "var1_20", 
"var1_30", "var2", "var3"), class = "data.frame", row.names = c(NA, 
-2L)) 
  • 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-18T07:49:47+00:00Added an answer on June 18, 2026 at 7:49 am

    Use a combination of merge, reshape, and unique as follows:

    unique(merge(DF[-c(3:4)], 
                 reshape(DF[1:4], direction = "wide", 
                         idvar = c("permno", "dte"), 
                         timevar="ttm")))
    #   permno         dte var2 var3 var1.20 var1.30
    # 1    123  2012-01-01   10  100       1      -1
    # 3    124  2012-01-01   20  200       2      -2
    

    Basically, you reshape only the columns that need to be reshaped, and drop those columns from the original dataset before merging. You’ll end up with duplicated rows, so just wrap all of that in unique to get (almost) your desired output. You can rearrange the column order if required.

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

Sidebar

Related Questions

Suppose that we have a data frame that looks like set.seed(7302012) county <- rep(letters[1:4],
I have a file that I would like to reshape it to use R:
table with data (its a data.table object) that looks like the following : date
I have a matrix that looks like this: SNP G1 G2 G3 marker1 TT
I have a database that looks like this: start<-as.POSIXct("2012-01-15") interval<-60 end<-start+as.difftime(31,units="days") date<-seq(from=start,by=interval*60, to=end) #
I'd like to reshape a data frame from long to wide (dummy data given
Would like a for loop in jquery so that: For every hover_link: show hidden
Would like to make anapplication in Java that will not automatically parse parameters used
I have some data represented in a 1300x1341 matrix. I would like to split
I have this data.frame with equal length groups ( id ) id | amount

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.