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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:40:40+00:00 2026-06-18T02:40:40+00:00

I have this data.frame with equal length groups ( id ) id | amount

  • 0

I have this data.frame with equal length groups (id)

id  |  amount 
--------------
 A  |   10   
 A  |   54   
 A  |   23   
 B  |   34   
 B  |   76    
 B  |   12    

which I would like to transpose by group id to this:

 id |
----------------------
 A  | 10  |  54 | 23  
 B  | 34  |  76 | 12

What is the most efficient way of doing this?

I’ve previously used reshape and dcast but they are very slow indeed! (I have A LOT of data and would love to speed up this bottleneck)

Is there a better strategy? Using data.table or matrices?? Any help would be much appreciated!

# Little data.frame
df <- data.frame(id=c(2,2,2,5,5,5), amount=as.integer(c(10,54,23,34,76,12)))

# Not so little data.frame
set.seed(10)
df <- data.frame(id = rep(sample(1:10000, 10000, replace=F),100), amount=as.integer(floor(runif(1000000, -100000,100000))))

# Create time variable
df$time <- ave(as.numeric(df$id), df$id, FUN = seq_along)

# The base R reshape strategy
system.time(df.reshape <-reshape(df, direction = "wide", idvar="id", timevar="time"))
user  system elapsed 
6.36    0.31    6.69 

# The reshape2 dcast strategy
require(reshape2)
a <- system.time(mm <- melt(df,id.vars=c('id','time'),measure.vars=c('amount')))
b <- system.time(df.dcast <- dcast(mm,id~variable+time,fun.aggregate=mean))
a+b
user  system elapsed 
14.44    0.00   14.45 

UPDATE
Using the fact that each group is equal in length you can use the matrix-function.

df.matrix <- data.frame(id=unique(df$id), matrix(df$amount, nrow=(length(unique(df$id))), byrow=T))
user  system elapsed 
0.03    0.00    0.03 

Note: This method assumes that the data.frame is presorted by id.

  • 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-18T02:40:41+00:00Added an answer on June 18, 2026 at 2:40 am

    This is not a problem of reshape. aggregate from base should be able to handle this.

    df.out <- aggregate(amount ~ id, data=df, c)
    # running on the small data
    #   id amount.1 amount.2 amount.3
    # 1  2       10       54       23
    # 2  5       34       76       12
    

    Isn’t this what you wanted?


    Okay, seems like an adapted version of DWin‘s solution is the fastest. However, the result will be ordered by id. If you don’t want that, then Aditya‘s seems to be the one to use.

    Here are the functions and the benchmarking results:

    • Using aggregate:

      AGG <- function() {
          df.agg <- aggregate(amount ~ id, data=df, c)
      }
      
    • Using Aditya‘s

      SEC <- function() {
          df.sec <- cbind(data.frame(id = unique(df$id)), 
                  matrix(as.numeric(unlist(tapply(df$amount, df$id, identity))), 
                  nrow = length(unique(df$id)), byrow = T))
      }
      
    • Using modified version of Dwin‘s:

      DWIN_M <- function() {
          df1 <- df[with(df, order(id)), ]
          idx <- df$id[!duplicated(df$id)]
          df.dwin <- cbind(data.frame(id=idx), 
                      as.data.frame(matrix(df1$amount, 
                      nrow=length(idx), byrow=TRUE)))
      }
      
    • Benchmarking:

      require(rbenchmark)
      benchmark(AGG(), SEC(), DWIN_M(), replications=3, order="elapsed")
      
      #      test replications elapsed relative user.self sys.self user.child sys.child
      # 3 DWIN_M()            3   4.175    1.000     4.148    0.000          0         0
      # 2    SEC()            3  17.568    4.208    17.449    0.016          0         0
      # 1    AGG()            3  24.529    5.875    24.306    0.044          0         0
      

    Let me know if I’ve made any errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this data frame. I would like to create another data frame from
I have a data frame with gaps like this: Var1 Var2 Var3 1 NA
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 numeric entries like this one test <- data.frame(x =
Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
I have the following data frame (info) that looks like this: > info[1:5,] field
I have a data.frame df like this: df <- data.frame (x=1:5,y=1:5) I want to
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 |

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.