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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:16:18+00:00 2026-06-18T14:16:18+00:00

Suppose I’ve got the following data frame : d <- data.frame(id=c(1,1,1,2,2,3,3,3), time=c(1,2,3,1,2,1,2,3), var=runif(8)) d

  • 0

Suppose I’ve got the following data frame :

d <- data.frame(id=c(1,1,1,2,2,3,3,3), time=c(1,2,3,1,2,1,2,3), var=runif(8))

 d
  id time       var
1  1    1 0.3733586
2  1    2 0.5743769
3  1    3 0.8253280
4  2    1 0.8136957
5  2    2 0.8726963
6  3    1 0.1105549
7  3    2 0.9527002
8  3    3 0.5690021

With the base reshape function, I can transform it to a “wide” format by specifying a ìdvar (which identifies rows belonging to the same unit) and a timevar (which identifies different observations of the same unit) :

reshape(d, idvar="id", timevar="time", direction="wide")

  id     var.1     var.2     var.3
1  1 0.3733586 0.5743769 0.8253280
4  2 0.8136957 0.8726963        NA
6  3 0.1105549 0.9527002 0.5690021

I’ve tried to do it with the dcast function of reshape2, but didn’t find a way. Do you know if it is possible ?

EDIT : Ananda Mahto’s comment and answer are perfectly right, the real question was to cast the original data frame when it has several var columns. My example was not appropriate, sorry.

  • 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-18T14:16:20+00:00Added an answer on June 18, 2026 at 2:16 pm

    Doesn’t the following work?

    dcast(d, id ~ time)
    # Using var as value column: use value.var to override.
    #   id         1          2         3
    # 1  1 0.2869739 0.59591690 0.8989719
    # 2  2 0.4533770 0.14741778        NA
    # 3  3 0.1286770 0.02465634 0.7363114
    
    ## OR, to get rid of the message:
    ## dcast(d, id ~ time, value.var = "var")
    

    I suspect, though, that you’re asking a little bit different question (as mentioned in my comment). In particular, what if you were starting with:

    set.seed(1)
    d <- data.frame(id = c(1,1,1,2,2,3,3,3), 
                    time = c(1,2,3,1,2,1,2,3), 
                    var1 = runif(8),
                    var2 = runif(8))
    

    With base R’s reshape, it’s just one line:

    reshape(d, direction = "wide", idvar = "id", timevar = "time")
    #   id    var1.1    var2.1    var1.2     var2.2    var1.3    var2.3
    # 1  1 0.2655087 0.6291140 0.3721239 0.06178627 0.5728534 0.2059746
    # 4  2 0.9082078 0.1765568 0.2016819 0.68702285        NA        NA
    # 6  3 0.8983897 0.3841037 0.9446753 0.76984142 0.6607978 0.4976992
    

    Let’s try the same with dcast from “reshape2”. Here’s the approach we might be tempted to take:

    library(reshape2)
    dcast(d, id ~ time)
    # Using var2 as value column: use value.var to override.
    #   id         1          2         3
    # 1  1 0.6291140 0.06178627 0.2059746
    # 2  2 0.1765568 0.68702285        NA
    # 3  3 0.3841037 0.76984142 0.4976992
    

    But that doesn’t work because dcast expects a single value.var. So, we need to melt the data again.

    d2 <- melt(d, id.vars = c("id", "time"))
    head(d2)
    #   id time variable     value
    # 1  1    1     var1 0.2655087
    # 2  1    2     var1 0.3721239
    # 3  1    3     var1 0.5728534
    # 4  2    1     var1 0.9082078
    # 5  2    2     var1 0.2016819
    # 6  3    1     var1 0.8983897
    

    Now, we can use dcast quite easily.

    dcast(d2, id ~ variable + time)
    #   id    var1_1    var1_2    var1_3    var2_1     var2_2    var2_3
    # 1  1 0.2655087 0.3721239 0.5728534 0.6291140 0.06178627 0.2059746
    # 2  2 0.9082078 0.2016819        NA 0.1765568 0.68702285        NA
    # 3  3 0.8983897 0.9446753 0.6607978 0.3841037 0.76984142 0.4976992
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a data frame with many columns and a particular summary procedure
Suppose this is a configuration data object that I read from DB/XML: class ConfigurationClass{
Suppose following class Structure: inside namespace a public abstract class A { protected int
Suppose you have the following controller action [HttpPost] public ActionResult Save( CustomerModel model )
Suppose I had the following method in an object: public class foo { public
Suppose the following table structure: Event: id: integer start_date: datetime end_date: datetime Is there
Suppose I've got two classes A and B such that B extends A ,
Suppose I have following class structure: class Base {} class A extends Base {}
Suppose there is a data field education in my table profile, now I want
Suppose the following function: float *dosomething(const float *src, const int N) { float *dst

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.