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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:50:56+00:00 2026-06-15T19:50:56+00:00

Background Now, I have a data frame shaped like: example = structure(list(sid = c(39,

  • 0

Background

Now, I have a data frame shaped like:

example = structure(list(sid = c(39, 40, 41, 42, 42, 43, 43, 44, 45, 45, 
46, 46, 47, 48, 49, 49, 50, 51, 52, 52, 53), monthday = c("42", 
"44", "46", "410", "428", "423", "49", "411", "416", "430", "418", 
"426", "419", "420", "420", "53", "421", "424", "425", "53", 
"511")), .Names = c("sid", "monthday"), row.names = c(301L, 300L, 
298L, 296L, 282L, 288L, 297L, 295L, 294L, 281L, 293L, 285L, 292L, 
291L, 290L, 278L, 289L, 287L, 286L, 279L, 270L), class = "data.frame")

In other words, it is tall:

sid   monthday  
39     42        
40     44         
41     46        
42    410        
42    428
43    423        
43     49

Ultimately, I would like to make it into a wide format:

sid   monthday1  monthday2
39     42         NA
40     44         NA
41     46         NA
42    410        428
43    423        49

etc

I’ve been trying things with reshape and reshape2 packages and also with aggregate like:

library(reshape2)
temp = melt(example,id.vars=c("sid"))
data.wide <- dcast(temp, sid ~ variable, value.var="value")

But can’t wrapp my brain around it. It occurs to me that if I could identify the occurance of each sid, I could solve my problem.

Immediate Problem

So how can take the tall data sid column above I make a new variable that indicates the occruence of each sid:

sid   occur 
39     1   
40     1   
41     1   
42     1
42     2 
43     1
43     2

the occur variable is indicating that sid values 39, 40, and 41 only appear once while 42 and 43 have first and second instances. If I only ever had two instances, I could use duplicated() and convert that to numeric, but what is a solution that can generalize to an arbitrary number of instances?

  • 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-15T19:50:58+00:00Added an answer on June 15, 2026 at 7:50 pm

    You can use ave to generate your “times”:

    example$time <- ave(example$sid, example$sid, FUN = seq_along)
    head(example)
    #     sid monthday time
    # 301  39       42    1
    # 300  40       44    1
    # 298  41       46    1
    # 296  42      410    1
    # 282  42      428    2
    # 288  43      423    1
    reshape(example, direction = "wide", idvar="sid", timevar="time")
    #    sid monthday.1 monthday.2
    # 301  39         42       <NA>
    # 300  40         44       <NA>
    # 298  41         46       <NA>
    # 296  42        410        428
    # 288  43        423         49
    # 295  44        411       <NA>
    # 294  45        416        430
    # 293  46        418        426
    # 292  47        419       <NA>
    # 291  48        420       <NA>
    # 290  49        420         53
    # 289  50        421       <NA>
    # 287  51        424       <NA>
    # 286  52        425         53
    # 270  53        511       <NA>
    

    Or, with dcast from “reshape2” after adding your time variable:

    dcast(example, sid ~ time, value.var="monthday")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have background image for a site header with fixed width. Now I am
Ok so I have a Background background-image: url('images/body.png'); now I want to overlay a
I have a big div with a big background-image. Now I want to create
Background I am developing an application for android in eclipse and now I have
I would like to create 10 plots that have different data, but the same
I have a JSON character string that I put into a data frame. I
I save data using binary serialization. Now I have changed a field in the
BACKGROUND: Right now, in our company we have a PHP based app to automate
I have a data frame with a quantitative variable, x, and several different factors,
I have a data table with alternating row background colors. I have an AJAX

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.