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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:12:15+00:00 2026-06-18T03:12:15+00:00

I have this data.frame: df <- data.frame(id=c(‘A’,’A’,’B’,’B’,’B’,’C’), amount=c(45,66,99,34,71,22)) id | amount ———– A |

  • 0

I have this data.frame:

df <- data.frame(id=c('A','A','B','B','B','C'), amount=c(45,66,99,34,71,22))

id | amount 
-----------
A  |   45   
A  |   66   
B  |   99
B  |   34 
B  |   71
C  |   22

which I need to expand so that each by group in the data.frame is of equal length (filling it out with zeroes), like so:

id | amount 
-----------
A  |   45   
A  |   66  
A  |   0     <- added 
B  |   99
B  |   34 
B  |   71
C  |   22
C  |   0     <- added 
C  |   0     <- added 

What is the most efficient way of doing this?

NOTE

Benchmarking the some of the solutions provided with my actual 1 million row data.frame I got:

             plyr   | data.table  |  unstack
          -----------------------------------
Elapsed:   139.87s  |    0.09s    |   2.00s
  • 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-18T03:12:16+00:00Added an answer on June 18, 2026 at 3:12 am

    One way using data.table

    df <- structure(list(V1 = structure(c(1L, 1L, 2L, 2L, 2L, 3L), 
              .Label = c("A  ", "B  ", "C  "), class = "factor"), 
              V2 = c(45, 66, 99, 34, 71, 22)), 
              .Names = c("V1", "V2"), 
              class = "data.frame", row.names = c(NA, -6L))
    
    require(data.table)
    dt <- data.table(df, key="V1")
    
    # get maximum index
    idx <- max(dt[, .N, by=V1]$N)
    
    # get final result
    dt[, list(V2 = c(V2, rep(0, idx-length(V2)))), by=V1]
    
    #     V1 V2
    # 1: A   45
    # 2: A   66
    # 3: A    0
    # 4: B   99
    # 5: B   34
    # 6: B   71
    # 7: C   22
    # 8: C    0
    # 9: C    0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame that looks like this: > head(ff.df) .id pio caremgmt prev
I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,
I have a dataframe with distances df<-data.frame(site.x=c(A,A,A,B,B,C), site.y=c(B,C,D,C,D,D),Distance=c(67,57,64,60,67,60)) I need to convert this to
I have a data.frame that looks like this > head(df) Memory Memory Memory Memory
i have a data frame that contains a data like this : V1 V2
I have this data.frame with equal length groups ( id ) id | amount
I have a data frame that looks like this: 'data.frame': 1090 obs. of 8
I have this huge data frame that has servernames, Date, CPU, memory as the
I have this dataframe DFtest <- data.frame(Zone=rep(c(R1,R2,R3),each=2), Type=rep(c(C1,C2),3), N1=sample(1:6), N2=sample(seq(0,1,length.out=6)), N3=sample(letters[1:6])) DFtest Zone Type

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.