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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:05:15+00:00 2026-06-17T23:05:15+00:00

(sorry if the title is not very informative: I don’t know how to define

  • 0

(sorry if the title is not very informative: I don’t know how to define better this question)

I have my data in the following form:

original data

In each group I have one pre value and one or two post values. I would like to convert this table to the following:

what I would like to get

I was thinking to group the data with something like:

aggregate(mydata, by = group, FUN = myfunction)

or

ddply(mydata, .(group), .fun = myfunction)

and process the elements of each group in my function. But I don’t see how to do this because I need to pass both type and value to my function simultaneously. Is there a better way to do this?

Update: quick-and-dirty sample dataset:

mydata <- data.frame(group = sample(letters[1:5], 10, replace = TRUE), 
                     type = sample(c("pre", "post"), 10, replace = TRUE), 
                     value = rnorm(10))
  • 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-17T23:05:17+00:00Added an answer on June 17, 2026 at 11:05 pm

    Try something like this:

    mydf <- data.frame(group = c("A", "A", "B", "B",
                                 "C", "C", "C", "D",
                                 "D", "E", "E"),
                       type = c("pre", "post", "pre",
                                "post", "pre", "post",
                                "post", "pre", "post",
                                "pre", "post"),
                       value = 1:11)
    
    times <- with(mydf, ave(value, group, type, FUN = seq_along))
    xtabs(value ~ group + interaction(type, times), mydf)
    #      interaction(type, times)
    # group post.1 pre.1 post.2 pre.2
    #     A      2     1      0     0
    #     B      4     3      0     0
    #     C      6     5      7     0
    #     D      9     8      0     0
    #     E     11    10      0     0
    

    Or:

    times <- with(mydf, ave(value, group, type, FUN = seq_along))  
    mydf$timevar <- interaction(mydf$type, times)
    reshape(mydf, direction = "wide", idvar = "group", 
            timevar="timevar", drop="type")
    #    group value.pre.1 value.post.1 value.post.2
    # 1      A           1            2           NA
    # 3      B           3            4           NA
    # 5      C           5            6            7
    # 8      D           8            9           NA
    # 10     E          10           11           NA
    

    The key, in both solutions, is to create a “time” variable that is represented by the combination of “type” and a sequence variable that can be created with ave.

    For completeness, here’s dcast from “reshape2”:

    times <- with(mydf, ave(value, group, type, FUN = seq_along))
    library(reshape2)
    dcast(mydf, group ~ type + times)
    #   group post_1 post_2 pre_1
    # 1     A      2     NA     1
    # 2     B      4     NA     3
    # 3     C      6      7     5
    # 4     D      9     NA     8
    # 5     E     11     NA    10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the title to this question, it's not very clear but wasn't sure
Sorry the title is not very clear. This is a follow up to my
Sorry the title may not be very clear. Basically I have a wrapper for
Sorry for a long question and not a very descriptive title, but my problem
Sorry if the title is not that specific, but I don't know how else
Sorry for the not very descriptive question title. I'm not very sure how to
I am sorry I didn't know how to title my question any better, you
Sorry, I wasn't sure exactly what title to give this and have not been
Sorry for the stupid title, but I don't know how else to name this
The title to this question isn't written very well. Sorry 'bout that. I'm curious

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.