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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:54:28+00:00 2026-06-07T01:54:28+00:00

I have a frame with many columns c1 c2 c3 c4 … c30 d

  • 0

I have a frame with many columns
c1 c2 c3 c4 … c30 d

I want to aggregate by and find all rows unique in c1..30 and then get the min(d) for that row. In sql this would be a group by c1, …, c30.

d is of type date.

I have found some solutions here in stack but none seem to work for 1) so many columns 2) do a min instead of sum.

Any input would be great.

  • 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-07T01:54:31+00:00Added an answer on June 7, 2026 at 1:54 am

    Here’s an answer using the data.table package with some fake data:

    library(data.table)
    
    DT<-data.table(matrix(sample(1:2,3000,replace=TRUE),ncol=30))
    DT2<-DT[sample(seq_len(nrow(DT)),9000,replace=TRUE)]
    # EDIT: now "d" is a date.
    DT2[,d:=as.POSIXct(origin = "1960-01-01",rnorm(nrow(DT2), sd = 1000))]
    setnames(DT2,c(paste0("c",1:30),"d"))
    
    ## pick up herewith your own data, starting with the commented next line
    # DT2 <- as.data.table(dataset)
    setkeyv(DT2,paste0("c",1:30))
    
    DT3<-DT2[,list(minD=min(d)),by=key(DT2)]
    
    dim(DT2)
    # [1] 9000   31
    dim(DT3)
    # [1] 100  31
    

    Small addition from Matthew :

    +10, and nice fake data. Setting a key first so you can do by=key(DT) can get a bit onerous sometimes, so I’d usually just do an ad hoc by for something like this for simplicity. But, trying the most natural thing first :

    DT2[,min(d),by=paste0("c",1:30)]
    Error in `[.data.table`(DT2, , min(d), by = paste0("c", 1:30)) : 
        'by' appears to evaluate to column names but isn't c() or key(). Use by=
        list(...) if you can. Otherwise, by=eval(paste0("c", 1:30)) should work.
        This is for efficiency so data.table can detect which columns are needed.
    

    The error message tells us what we need to do instead :

    ans = DT2[,min(d),by=eval(paste0("c",1:30))]
    dim(ans)
    [1] 100  31
    

    The natural next thought is of course: well, if data.table is clever enough to know by is column names and put that in the error message, why can’t it just do it? The answer is that it’s only making a guess based on the data. In some edge cases it’s not so clear. So currently extra intent is needed from user: wrapping with eval. I’m not completely happy with that though, so perhaps we can improve that.

    EDIT: renaming the new data.table

    In my approach, I named the new column minD when I created it by entering

    DT3<-DT2[,list(minD=min(d)),by=key(DT2)]
    

    Using Matthew Dowle’s approach, you would achieve this pretty much the same way by entering

    ans = DT2[,list(minD=min(d)),by=eval(paste0("c",1:30))]
    

    If you’ve already created the column and want to rename it, use setnames as follows:

    setnames(DT3,old="minD",new="theNewMinD")
    

    This avoids copying the whole data.table and preserves the memory over allocation (both of these advantages are lost when using names(DT3)<-"something"), as outlined in the documentation under ?setnames

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

Sidebar

Related Questions

Suppose that you have a data frame with many rows and many columns. The
I have a data frame with an id column and some (potentially many) columns
I have a dataframe with 6 columns and many rows that includes positions for
I have a dataset (Purchase.df) that contains many columns and rows. The important variable
I have a data.frame with 1,000 rows and 3 columns. It contains a large
I have many data.frames, for example: df1 = data.frame(names=c('a','b','c','c','d'),data1=c(1,2,3,4,5)) df2 = data.frame(names=c('a','e','e','c','c','d'),data2=c(1,2,3,4,5,6)) df3 =
I have a frame extends from JWindow (because I want to handle my X
I have a data frame full from which I want to take the last
Problem I want to melt a dataframe with many columns, using very little typing.
I have a data frame consisting of the columns; name, time, count. Showing how

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.