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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:49:09+00:00 2026-05-26T22:49:09+00:00

I know this has been asked numerous times on here under the rubric of

  • 0

I know this has been asked numerous times on here under the rubric of “long to wide” but I’ve run into a situation where I have two value variables that are repeated measures.

          id sex  time      score1    score2
1  subject 1   m Time1 -0.20926263 0.2499310
2  subject 2   m Time1  0.17147511 3.2708905
3  subject 3   m Time1 -0.82619584 0.5993917
4  subject 4   f Time1 -0.95568823 4.4729726
5  subject 5   f Time1 -2.29939525 8.0101254
6  subject 1   m Time2 -0.37914702 3.6387589
7  subject 2   m Time2  0.26759909 4.9027533
8  subject 3   m Time2  0.07727621 2.1848642
9  subject 4   f Time2 -0.08613439 5.8747074
10 subject 5   f Time2 -0.02743044 4.3963938
11 subject 1   m Time3  0.07176053 3.7959496
12 subject 2   m Time3  0.46463917 5.2494579
13 subject 3   m Time3 -0.68764512 2.2639503
14 subject 4   f Time3 -0.56670061 2.3361909
15 subject 5   f Time3  1.70731774 5.8345116

Quick way to reproduce the data frame (DF).

DF<-data.frame(id=rep(paste("subject", 1:5, sep=" "), 3),              
               sex=rep(c("m","m","m","f","f"), 3),                     
               time=c(rep("Time1",5), rep("Time2",5), rep("Time3",5)), 
               score1=rnorm(15), score2=abs(rnorm(15)*4))              

I can solve the issue of long to wide for two measured repeated measure variables using the reshape function from base but I was hoping for a plyr or reshape2/1 answer, as these packages are generally much more intuitive to me. If you have any other solutions go ahead and provide them as the learning would be great.

Solution from base:

wide <- reshape(DF, v.names=c("score1", "score2"), idvar="id",         
                timevar="time", direction="wide")                      
wide   
  • 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-05-26T22:49:10+00:00Added an answer on May 26, 2026 at 10:49 pm

    I think this will do it:

    library(reshape)
    m <- melt(DF)
    

    Simplest, but time and score are in the opposite order from your example (in case it matters)

    cast(m,id+sex~...)
    

    Or more explicitly:

    cast(m,id+sex~variable+time)
    

    You can cut this down to a one-liner:

    recast(DF,id+sex~...)
    

    If you like you can use the newer reshape2 package instead of reshape, replacing cast with dcast (the version of recast included in reshape2 doesn’t give the desired result.)

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

Sidebar

Related Questions

I know this question has been asked numerous number of times, both here and
I know this has been asked a few times here. But none of the
I know this question has been asked numerous times but I couldn't find a
I know this has been asked a few times around here but most of
I know this has been asked multiple times here, but I've a different issue
I know this question has been asked numerous times but I've tried everything. I
I know this has been asked a dozen times here at stackoverflow, but it
I know this has been asked a hundred times on here, but I've been
I know this question has been asked before numorous times but they seem to
I know this has been asked, but none of the numerous answers fit with

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.