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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:08:42+00:00 2026-06-13T18:08:42+00:00

I need to reshape from long into wide format. I do not have a

  • 0

I need to reshape from long into wide format. I do not have a times variable. What is the easiest way to create a time variable for each id in the following dataset for subsequent reshaping in base r (not the reshape package)?

h<-seq(from=as.Date("2005-06-01"), to=as.Date("2008-06-30"), by=1)

a<-data.frame(id=sample(c(1:100),300,replace=T),val=rnorm(n=300),date=sample(h,300,replace=T))

//M

  • 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-13T18:08:44+00:00Added an answer on June 13, 2026 at 6:08 pm

    Here is one possible approach. It uses ave to create a “time” variable according to how many times an “id” occurs, which, to me, sounds like what you’re looking for.

    Your data, but ordered (and using set.seed so others can reproduce it):

    set.seed(1)
    h <- seq(from=as.Date("2005-06-01"), 
             to=as.Date("2008-06-30"), by=1)
    a <- data.frame(id=sample(c(1:100), 300, replace=TRUE),
                    val=rnorm(n=300), 
                    date=sample(h, 300, replace=TRUE))
    rm(h)
    a <- a[order(a$id, a$date), ]
    rbind(head(a), tail(a))
    #      id         val       date
    # 27    2  0.78763961 2007-08-25
    # 116   2  0.27005490 2008-03-05
    # 281   3 -2.03328560 2006-08-08
    # 47    3  1.44115771 2007-06-25
    # 133   4  1.32425863 2006-06-14
    # 228   5 -0.14587563 2006-10-15
    # 111  98  0.95101281 2008-04-29
    # 293  99 -0.01825971 2006-01-20
    # 139  99  0.43370215 2008-02-20
    # 121 100 -0.25893258 2005-06-07
    # 18  100 -1.42449465 2007-08-19
    # 104 100 -0.24766434 2008-05-11
    

    You’ll end up with 8 “times”, checked by using table.

    max(table(a$id))
    # [1] 8
    a$time <- ave(a$id, a$id, FUN=seq_along)
    rbind(head(a), tail(a))
    #      id         val       date time
    # 27    2  0.78763961 2007-08-25    1
    # 116   2  0.27005490 2008-03-05    2
    # 281   3 -2.03328560 2006-08-08    1
    # 47    3  1.44115771 2007-06-25    2
    # 133   4  1.32425863 2006-06-14    1
    # 228   5 -0.14587563 2006-10-15    1
    # 111  98  0.95101281 2008-04-29    1
    # 293  99 -0.01825971 2006-01-20    1
    # 139  99  0.43370215 2008-02-20    2
    # 121 100 -0.25893258 2005-06-07    1
    # 18  100 -1.42449465 2007-08-19    2
    # 104 100 -0.24766434 2008-05-11    3
    a.wide <- reshape(a, direction = "wide", idvar="id", timevar="time")
    a.wide[1:8, 1:8]
    #     id      val.1     date.1      val.2     date.2     val.3     date.3    val.4
    # 27   2  0.7876396 2007-08-25  0.2700549 2008-03-05        NA       <NA>       NA
    # 281  3 -2.0332856 2006-08-08  1.4411577 2007-06-25        NA       <NA>       NA
    # 133  4  1.3242586 2006-06-14         NA       <NA>        NA       <NA>       NA
    # 228  5 -0.1458756 2006-10-15  0.5929847 2008-03-31        NA       <NA>       NA
    # 299  6  0.2368037 2006-02-06  1.0341077 2006-10-07        NA       <NA>       NA
    # 10   7  1.8692906 2005-07-19 -0.4839749 2006-06-02  1.435070 2007-11-30 1.017754
    # 158  8  0.5672209 2006-08-28 -0.4075286 2006-11-11 -2.285236 2007-03-29       NA
    # 69   9  0.6422413 2008-06-20         NA       <NA>        NA       <NA>       NA
    names(a.wide)
    #  [1] "id"     "val.1"  "date.1" "val.2"  "date.2" "val.3"  "date.3" "val.4"  
    #  [9] "date.4" "val.5"  "date.5" "val.6"  "date.6" "val.7"  "date.7" "val.8" 
    # [17] "date.8"
    
    • 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 in panel format (country-year) and I need to calculate the
I have a 2D masked array of values that I need to sort from
Need some regular expressions help. So far I have my code working to allow
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need help with a query that I wrote: I have three tables Company id
Need a list of Pcap.Net members or classes? Their website doesn't have much documentation
Need a way to navigate/browse XSLT files easily with Vim. Similar to the way
Need help with the codeigniter, I think file_exists is for server path, not for
I am somewhat new to R and I have run into a point where
Need help with PHP/MySql. Need to select all the records from 'today'. My table

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.