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

  • Home
  • SEARCH
  • 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 8924893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:32:44+00:00 2026-06-15T07:32:44+00:00

I’ve looked all over the web including StackOverflow, and tested various things before asking

  • 0

I’ve looked all over the web including StackOverflow, and tested various things before asking this question, but pardon me if I missed an excellent answer.

I see lots of help for the reshape function (and the package too, but I can’t get either to do what I need). I have a “time” variable that differs by subject, e.g., it is not time1, time2, time3. I would like to make a wide data set that treats each unique time value by subject ID as just “time1”, “time2”, “time3”, but I need to save the dates. To make this concrete, here is some sample data:

Id<-c(1, 1,1, 2,2,2, 3)
date<-c("Jan10", "Jun11", "Dec11", "Feb10", "May10", "Dec10", "Jan11")
Score<-c(52, 43, 67, 56, 33, 21, 20)
format2<-data.frame(Id, date, Score)
format2

  Id  date Score
1  1 Jan10    52
2  1 Jun11    43
3  1 Dec11    67
4  2 Feb10    56
5  2 May10    33
6  2 Dec10    21
7  3 Jan11    20

I would like it to look like this:

  Id  date1 Score1 date2 Score2 date3 Score3
  1  Jan10    52    Jun11    43  Dec11    67
  2  Feb10    56    Dec10    21  May10    33 
  3  Jan11    20      NA     NA    NA     NA

Thank you for any help and my apologies if I have missed an obvious answer.

  • 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-15T07:32:46+00:00Added an answer on June 15, 2026 at 7:32 am

    You need to generate a time variable, which can be done quickly using ave():

    format2$time <- ave(format2$Id, format2$Id, FUN=seq_along)
    reshape(format2, direction = "wide", idvar="Id", timevar="time")
    #   Id date.1 Score.1 date.2 Score.2 date.3 Score.3
    # 1  1  Jan10      52  Jun11      43  Dec11      67
    # 4  2  Feb10      56  May10      33  Dec10      21
    # 7  3  Jan11      20   <NA>      NA   <NA>      NA
    

    Some people prefer the reshape2 package because of its syntax, but even there, you need to have a time variable before you can do anything interesting.

    Continuing from above (where the time variable was created):

    library(reshape2)
    format2m <- melt(format2, id.vars=c("Id", "time"))
    dcast(format2m, Id ~ variable + time)
    #   Id date_1 date_2 date_3 Score_1 Score_2 Score_3
    # 1  1  Jan10  Jun11  Dec11      52      43      67
    # 2  2  Feb10  May10  Dec10      56      33      21
    # 3  3  Jan11   <NA>   <NA>      20    <NA>    <NA>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.