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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:46:08+00:00 2026-05-26T06:46:08+00:00

I have a data set like this: structure(list(var1 = c(APE, APE, APE, APE, APE,

  • 0

I have a data set like this:

structure(list(var1 = c("APE", "APE", "APE", "APE", "APE", "APE", "GIT",
"APE", "APE", "APE", "APE", "APE", "APE", "APE", "GIT"), var2 = c("AVVAL",
"AULASU", "APALA", "AEA", "ATUPVA", "ASATAP", "ADLO", "AKOKU", "AVVAL",
"AULASU", "APALA", "AEA", "ATUPVA", "ASATAP", "ADLO"), var3 = c(NA,
NA, 1L, 101L, 17122009L, 1L, NA, 684L, NA, NA, 1L, 10L, 17122L,
1L, NA)), .Names = c("var1", "var2", "var3"), row.names = c(NA,
15L), class = "data.frame")

How can I reshape this data into wide format? I tried this

reshape(h, idvar="var2", v.names="var3", timevar="var1", direction="wide")

but it is not giving me a correct results. The correct result is:

  var1 ADLO AEA AKOKU APALA ASATAP   ATUPVA AULASU AVVAL
1  APE  NaN 101   NA      1      1 17122009     NA    NA
2  APE  NaN  10   684     1      1    17122     NA    NA
3  GIT   NA NaN   NaN   NaN    NaN      NaN    NaN   NaN
4  GIT   NA NaN   NaN   NaN    NaN      NaN    NaN   NaN
  • 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-26T06:46:09+00:00Added an answer on May 26, 2026 at 6:46 am

    Edited

    The only way I can get to your expected results is to add a new column to the data.frame. It seems to me that there is some information implicit about your data that isn’t contained in the data. In other words, there must be some kind of grouping variable that identifies certain records as belonging together.

    Since I can’t double-guess what this information is, in my answer I am going to assume that each occurrence of GIT marks the end of a record:

    x <- grep("GIT", h$var1)
    h$rec <- rep(seq_along(x), times=c(x[1], diff(x)))
    
    library(reshape2)
    mh <- melt(h, measure.vars="var3")
    cast(mh, rec+var1~var2, id.var="rec", measure.var="value", fun.aggregate=mean)
    
      rec var1 ADLO AEA AKOKU APALA ASATAP   ATUPVA AULASU AVVAL
    1   1  APE  NaN  10   NaN     1      1 17122009     NA    NA
    2   1  GIT   NA NaN   NaN   NaN    NaN      NaN    NaN   NaN
    3   2  APE  NaN  10   684     1      1 17122009     NA    NA
    4   2  GIT   NA NaN   NaN   NaN    NaN      NaN    NaN   Na
    

    Original answer

    I find the package reshape2 much easier to comprehend than the built-in reshape function. This package provides two functions:

    • melt to make a wide data.frame tall
    • cast to make a tall data.frame wide

    In your case you need cast:

    library(reshape2)
    cast(h, var1~var2, value="var3", fun.aggregate=mean)
    
      var1 ADLO AEA AKOKU APALA ASATAP   ATUPVA AULASU AVVAL
    1  APE  NaN  10   684     1      1 17122009     NA    NA
    2  GIT   NA NaN   NaN   NaN    NaN      NaN    NaN   NaN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I have a data set that looks like this: 000 100 200 300 010
I have a data structure that represents C# code like this: class Namespace: string
I have a simple set of arrays, kind of like this .. structure shortened
I have a data structure that looks like this: public class Node { public
I have several complex data structures like Map< A, Set< B > > Set<
I have a set of survey data, and I'd like to generate plots of
I have a set of controls bound to data, on which I would like
I have a set of core, complicated JavaScript data structures/classes that I'd like to
This question is more a semantic-algorithmic-data-structure question than a F# syntactically question. I have

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.