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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:04:02+00:00 2026-06-17T22:04:02+00:00

I need to turn this id | amount | day ——————— A | 10

  • 0

I need to turn this

id  |  amount |  day
---------------------
 A  |   10    |  0 
 A  |   54    |  8
 A  |   23    |  18
 A  |   43    |  28
 A  |   87    |  51
 B  |   34    |  0
 B  |   76    |  1 
 B  |   12    |  7

into this

id | a1 | a2 | a3 | a4 | a5 | d1 | d2 | d3 | d4 | d5 
--------------------------------------------------------
 A | 10 | 54 | 23 | 43 | 87 | 0  |  8 | 18 | 28 | 51
 B | 34 | 76 | 12 | 0  |  0 | 0  |  1 |  7 | 0  | 0

ie. transpose/cast the rows of the df to an unknown number of columns by id, putting zeros where there would be empty values because of the inequal length.

I’ve experimented with

df <- data.frame(id=c('A','A','A','A','A','B','B','B'),    amount=c(10,54,23,43,87,34,76,12), day=c(0,8,18,28,51,0,1,7))
library(reshape2)
x <- dcast(df, id ~ day, mean, value = 'amount')

but it’s not quite right. How do I do it?

  • 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-17T22:04:03+00:00Added an answer on June 17, 2026 at 10:04 pm

    Use base R reshape() after introducing a “time” variable:

    df$time <- ave(as.numeric(as.character(df$id)), df$id, FUN = seq_along)
    df
    #   id amount day time
    # 1  A     10   0    1
    # 2  A     54   8    2
    # 3  A     23  18    3
    # 4  A     43  28    4
    # 5  A     87  51    5
    # 6  B     34   0    1
    # 7  B     76   1    2
    # 8  B     12   7    3
    reshape(df, direction = "wide", idvar="id", timevar="time")
    #   id amount.1 day.1 amount.2 day.2 amount.3 day.3 amount.4 day.4 amount.5 day.5
    # 1  A       10     0       54     8       23    18       43    28       87    51
    # 6  B       34     0       76     1       12     7       NA    NA       NA    NA
    

    Optional steps:

    1. Reorganize column order:

      df2 <- df2[c("id", 
                   grep("amount", names(df2), value=TRUE), 
                   grep("day", names(df2), value = TRUE))]
      
    2. Replace NA with 0:

      df2[is.na(df2)] <- 0
      df2
      #   id amount.1 amount.2 amount.3 amount.4 amount.5 day.1 day.2 day.3 day.4 day.5
      # 1  A       10       54       23       43       87     0     8    18    28    51
      # 6  B       34       76       12        0        0     0     1     7     0     0
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to turn this phone number: 5555555555 into this string with delimiters: 555.555.5555
How can I turn this into an array? I need a board to show
I have an projector file/Flash application that I need to turn into an interactive
I need to split a keyword string and turn it into a comma delimited
I need to turn this on/enable it. I found some info on the net
I need to turn this JSON object: [ [ email@email.com ], [ email2@email.com ],
In php, I need to turn this array: array( 'group1_selector1_rule1' => value , 'group1_selector1_rule2'
Please not not turn this into a discussion about which browser is better and
I'm using jqPlot and I need to turn this JSON which I receive from
I need to turn off the gsm network at my application but enable bluetooth.

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.