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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:06:34+00:00 2026-06-17T05:06:34+00:00

I am trying to reshape the following dataset with reshape() , without much results.

  • 0

I am trying to reshape the following dataset with reshape(), without much results.

The starting dataset is in “wide” form, with each id described through one row. The dataset is intended to be adopted for carry out Multistate analyses (a generalization of Survival Analysis).

Each person is recorded for a given overall time span. During this period the subject can experience a number of transitions among states (for simplicity let us fix to two the maximum number of distinct states that can be visited). The first visited state is s1 = 1, 2, 3, 4. The person stays within the state for dur1 time periods, and the same applies for the second visited state s2:

   id    cohort    s1     dur1     s2     dur2     
     1      1        3      4       2      5       
     2      0        1      4       4      3    

The dataset in long format which I woud like to obtain is:

id    cohort    s    
1       1       3
1       1       3
1       1       3
1       1       3
1       1       2
1       1       2
1       1       2
1       1       2
1       1       2
2       0       1
2       0       1
2       0       1
2       0       1
2       0       4
2       0       4
2       0       4

In practice, each id has dur1 + dur2 rows, and s1 and s2 are melted in a single variable s.

How would you do this transformation? Also, how would you cmoe back to the original dataset “wide” form?

Many thanks!

dat <- cbind(id=c(1,2), cohort=c(1, 0), s1=c(3, 1), dur1=c(4, 4), s2=c(2, 4), dur2=c(5, 3))
  • 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-17T05:06:35+00:00Added an answer on June 17, 2026 at 5:06 am

    You can use reshape() for the first step, but then you need to do some more work. Also, reshape() needs a data.frame() as its input, but your sample data is a matrix.

    Here’s how to proceed:

    1. reshape() your data from wide to long:

      dat2 <- reshape(data.frame(dat), direction = "long", 
                      idvar = c("id", "cohort"),
                      varying = 3:ncol(dat), sep = "")
      dat2
      #       id cohort time s dur
      # 1.1.1  1      1    1 3   4
      # 2.0.1  2      0    1 1   4
      # 1.1.2  1      1    2 2   5
      # 2.0.2  2      0    2 4   3
      
    2. “Expand” the resulting data.frame using rep()

      dat3 <- dat2[rep(seq_len(nrow(dat2)), dat2$dur), c("id", "cohort", "s")]
      dat3[order(dat3$id), ]
      #         id cohort s
      # 1.1.1    1      1 3
      # 1.1.1.1  1      1 3
      # 1.1.1.2  1      1 3
      # 1.1.1.3  1      1 3
      # 1.1.2    1      1 2
      # 1.1.2.1  1      1 2
      # 1.1.2.2  1      1 2
      # 1.1.2.3  1      1 2
      # 1.1.2.4  1      1 2
      # 2.0.1    2      0 1
      # 2.0.1.1  2      0 1
      # 2.0.1.2  2      0 1
      # 2.0.1.3  2      0 1
      # 2.0.2    2      0 4
      # 2.0.2.1  2      0 4
      # 2.0.2.2  2      0 4 
      

    You can get rid of the funky row names too by using rownames(dat3) <- NULL.

    Update: Retaining the ability to revert to the original form

    In the example above, since we dropped the “time” and “dur” variables, it isn’t possible to directly revert to the original dataset. If you feel this is something you would need to do, I suggest keeping those columns in and creating another data.frame with the subset of the columns that you need if required.

    Here’s how:

    Use aggregate() to get back to “dat2”:

    aggregate(cbind(s, dur) ~ ., dat3, unique)
    #   id cohort time s dur
    # 1  2      0    1 1   4
    # 2  1      1    1 3   4
    # 3  2      0    2 4   3
    # 4  1      1    2 2   5
    

    Wrap reshape() around that to get back to “dat1”. Here, in one step:

    reshape(aggregate(cbind(s, dur) ~ ., dat3, unique), 
            direction = "wide", idvar = c("id", "cohort"))
    #   id cohort s.1 dur.1 s.2 dur.2
    # 1  2      0   1     4   4     3
    # 2  1      1   3     4   2     5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to randomly generate one div from an array on each load of a
I am trying to use the functionality of the reshape function available in MATLAB
I am working with a dataset that comes with lme4, and am trying to
Trying to load a custom form submisson error box in Facebox, when testing around
I have two images I'm trying to co-register - ie, one could be of
Trying to learn asio, and I'm following the examples from the website. Why is
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
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Trying to make this jQuery filter that uses .find case-insensitive. For example, when 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.