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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:45:48+00:00 2026-06-04T01:45:48+00:00

I want to determine the column classes of a large data.table. colClasses <- sapply(DT,

  • 0

I want to determine the column classes of a large data.table.

colClasses <- sapply(DT, FUN=function(x)class(x)[1])

works, but apparently local copies are stored into memory:

> memory.size()
[1] 687.59
> colClasses <- sapply(DT, class)
> memory.size()
[1] 1346.21

A loop seems not possible, because a data.table “with=FALSE” always results in a data.table.

A quick and very dirty method is:

DT1 <- DT[1, ]
colClasses <- sapply(DT1, FUN=function(x)class(x)[1])

What is the most elegent and efficient way to do this?

  • 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-04T01:45:49+00:00Added an answer on June 4, 2026 at 1:45 am

    Have briefly investigated, and it looks like a data.table bug.

    > DT = data.table(a=1:1e6,b=1:1e6,c=1:1e6,d=1:1e6)
    > Rprofmem()
    > sapply(DT,class)
            a         b         c         d 
    "integer" "integer" "integer" "integer" 
    > Rprofmem(NULL)
    > noquote(readLines("Rprofmem.out"))
    [1] 4000040 :"as.list.data.table" "as.list" "lapply" "sapply"       
    [2] 4000040 :"as.list.data.table" "as.list" "lapply" "sapply" 
    [3] 4000040 :"as.list.data.table" "as.list" "lapply" "sapply"   
    [4] 4000040 :"as.list.data.table" "as.list" "lapply" "sapply" 
    
    > tracemem(DT)
    > sapply(DT,class)
    tracemem[000000000431A290 -> 00000000065D70D8]: as.list.data.table as.list lapply sapply 
            a         b         c         d 
    "integer" "integer" "integer" "integer" 
    

    So, looking at as.list.data.table :

    > data.table:::as.list.data.table
    function (x, ...) 
    {
        ans <- unclass(x)
        setattr(ans, "row.names", NULL)
        setattr(ans, "sorted", NULL)
        setattr(ans, ".internal.selfref", NULL)
        ans
    }
    <environment: namespace:data.table>
    > 
    

    Note the pesky unclass on the first line. ?unclass confirms that it takes a deep copy of its argument. From this quick look it doesn’t seem like sapply or lapply are doing the copying (I didn’t think they did since R is good at copy-on-write, and those aren’t writing), but rather the as.list in lapply (which dispatches to as.list.data.table).

    So, if we avoid the unclass, it should speed up. Let’s try:

    > DT = data.table(a=1:1e7,b=1:1e7,c=1:1e7,d=1:1e7)
    > system.time(sapply(DT,class))
       user  system elapsed 
       0.28    0.06    0.35 
    > system.time(sapply(DT,class))  # repeat timing a few times and take minimum
       user  system elapsed 
       0.17    0.00    0.17 
    > system.time(sapply(DT,class))
       user  system elapsed 
       0.13    0.04    0.18 
    > system.time(sapply(DT,class))
       user  system elapsed 
       0.14    0.03    0.17 
    > assignInNamespace("as.list.data.table",function(x)x,"data.table")
    > data.table:::as.list.data.table
    function(x)x
    > system.time(sapply(DT,class))
       user  system elapsed 
          0       0       0 
    > system.time(sapply(DT,class))
       user  system elapsed 
       0.01    0.00    0.02 
    > system.time(sapply(DT,class))
       user  system elapsed 
          0       0       0 
    > sapply(DT,class)
            a         b         c         d 
    "integer" "integer" "integer" "integer" 
    > 
    

    So, yes, infinitely better.

    I’ve raised bug report #2000 to remove the as.list.data.table method, since a data.table is() already a list, too. This might speed up quite a few idioms actually, such as lapply(.SD,...). [EDIT: This was fixed in v1.8.1].

    Thanks for asking this question!!

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

Sidebar

Related Questions

I want to determine if a column exists in a table for any jdbc
i want a table to render with minimum width according to its content, but
Generally we traverse the array by row or column but here I want to
I want to index a computed column in my database table which uses a
is there a way to programatically determine the EJB entity class and id column
I want to use field name to determine the column that I will use
I have a ftp client that allows users to upload files. I want determine
I want to determine which part of audio file contain speech or music. I
I want to determine the screen resolution of the client machine and the set
I want to determine the scroll of the users. I'm using jQuery.. And jquery

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.