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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:42:29+00:00 2026-06-16T09:42:29+00:00

I have a data.table DT and I want to run model.matrix on it. Each

  • 0

I have a data.table DT and I want to run model.matrix on it. Each row has a string ID, which is stored in the ID column of DT. When I run model.matrix on DT, my formula excludes the ID column. The problem is, model.matrix drops some rows because of NAs. If I set the rownames of DT to the ID column, before calling model.matrix, then the final model matrix has rownames, and I’m all set. Otherwise, I can’t figure out what rows I end up with. I’m setting the rownames with rownames(DT) = DT$ID. However, when I try to add a new column to DT, I get a complaint about

“Invalid .internal.selfref detected . . . At an earlier point, this
data.table has been copied by R.”

So I’m wondering

  1. Is there a better way to set rownames for a data.table
  2. Is there a better approach to solving this problem.
  • 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-16T09:42:30+00:00Added an answer on June 16, 2026 at 9:42 am

    There are a couple of issues here.

    Firstly, it is a feature of a data.table that they do not have a rownames, instead they have keys which are far more powerful. See this great vignette.

    But, it isn’t the end of the world. model.matrix returns sensible rownames when you pass it a data.table

    For example

    A <- data.table(ID = 1:5, x = c(NA, 1:4), y = c(4:2,NA,3))
    
    mm <- model.matrix( ~ x + y, A)
    
    rownames(mm)
    
    ## [1] "2" "3" "5"
    

    So rows 2,3 and 5 are those included in the model.matrix.

    Now, you can add this sequence as a column to A. This will be useful if you then set the key to something else (thereby losing the original order)

    A[, rowid := seq_len(nrow(A)]
    

    You might consider making it character (like the rownames of mm)) but it won’t really matter (as you can just as easily convert rownames(mm) to numeric when you need to reference.

    As to the warning that data.table gives, if you read the next sentence

    Avoid key<-, names<- and attr<- which in R currently (and oddly) may copy the whole data.table. Use set* syntax instead to avoid copying: setkey(), setnames() and setattr()

    rownames are an attribute rownames<- (internally at somepoint using the equivalent to attr<-) will (possibly copy) in the same way.

    The line from `row.names<-.data.frame` is

    attr(x, "row.names") <- value
    

    That being said, data.tables don’t have rownames, so there is no point setting them.

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

Sidebar

Related Questions

I have about 1 million data in users table. Now I want each user
I have the data table from the jquery plugin dataTables (http://datatables.net/) that I want
I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
I have a simple database named customer with a single table data.I want to
I have data in 2 tables, and I want to create a report. Table
I have created one jTable. I want to display the data into table from
I have a table of data with survey results, and I want to do
I have a scripts that retrieves huge data on a table. I want to
I have a trigger that I want to run before insert a row into
I have a dynamic range in an Excel sheet. DATA_TABLE =OFFSET(DATA!$B$3,0,0,COUNTA(DATA!$B:$B)-1,0) I want to

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.