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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:14:54+00:00 2026-06-09T15:14:54+00:00

I’m currently trying to get used to data.table package in R . I want

  • 0

I’m currently trying to get used to data.table package in R. I want to get the index for the last 1 ocurring in each row of a data table, say a and add that new column to a. My code for this is the following:

  a = data.table(matrix(sample(c(0,1),500,rep=T),50,10))
  a[,ind:=apply(a==1,1,function(x) max(which(x)))]

Nevertheless, I think this can be written in a short way using more data.table syntax. Therefore, my question is: how to do this without the apply function within the j component of [?

  • 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-09T15:14:55+00:00Added an answer on June 9, 2026 at 3:14 pm

    Great question. Yes, the apply by row isn’t page efficient, the which will be allocating for each and every row, and the a==1 creates a new logical matrix as large as a.

    In data.table we do things by column. Sometimes, it’s data.table-ish to use a for loop through columns (never for loop through rows) :

    a[,ans:=0L]
    for (i in 1:10) set(a,which(a[[i]]==1),"ans",i)
    
    identical(a$ind, a$ans)
    # [1] TRUE
    

    As you can see it’s a completely different style. But, I think, this should be :

    • page efficient; i.e., it runs by columns which are contiguous in memory
    • needs working memory as large as (just) one column rather than whole of a
    • calls which() (a non primitive, vectorized function) 10 rather than nrow(a) times

    I didn’t do any speed tests, though, so I might have to eat my words.

    See ?set.

    In response to comment, to inspect how it works, set happens to return a pointer to the data.table, so we can look at the first few rows as it progresses.

    a[,ans:=0L]   # add column by reference, initialized with 0L
    
    > head(a)
       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 ans
    1:  0  0  1  0  1  1  0  1  1   1   0
    2:  0  0  1  1  0  0  1  1  1   1   0
    3:  0  1  0  0  0  1  1  1  1   0   0
    4:  0  0  0  1  1  0  1  1  1   1   0
    5:  1  1  1  1  0  0  0  0  0   1   0
    6:  1  1  0  1  1  0  1  0  1   1   0
    

    Now hopefully the following reveals how it’s working :

    > for (i in 1:10) print(head(set(a,which(a[[i]]==1),"ans",i)))
       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 ans
    1:  0  0  1  0  1  1  0  1  1   1   0
    2:  0  0  1  1  0  0  1  1  1   1   0
    3:  0  1  0  0  0  1  1  1  1   0   0
    4:  0  0  0  1  1  0  1  1  1   1   0
    5:  1  1  1  1  0  0  0  0  0   1   1
    6:  1  1  0  1  1  0  1  0  1   1   1
       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 ans
    1:  0  0  1  0  1  1  0  1  1   1   0
    2:  0  0  1  1  0  0  1  1  1   1   0
    3:  0  1  0  0  0  1  1  1  1   0   2
    4:  0  0  0  1  1  0  1  1  1   1   0
    5:  1  1  1  1  0  0  0  0  0   1   2
    6:  1  1  0  1  1  0  1  0  1   1   2
       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 ans
    1:  0  0  1  0  1  1  0  1  1   1   3
    2:  0  0  1  1  0  0  1  1  1   1   3
    3:  0  1  0  0  0  1  1  1  1   0   2
    4:  0  0  0  1  1  0  1  1  1   1   0
    5:  1  1  1  1  0  0  0  0  0   1   3
    6:  1  1  0  1  1  0  1  0  1   1   2
       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 ans
    1:  0  0  1  0  1  1  0  1  1   1   3
    2:  0  0  1  1  0  0  1  1  1   1   4
    3:  0  1  0  0  0  1  1  1  1   0   2
    4:  0  0  0  1  1  0  1  1  1   1   4
    5:  1  1  1  1  0  0  0  0  0   1   4
    6:  1  1  0  1  1  0  1  0  1   1   4
       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 ans
    1:  0  0  1  0  1  1  0  1  1   1   5
    2:  0  0  1  1  0  0  1  1  1   1   4
    3:  0  1  0  0  0  1  1  1  1   0   2
    4:  0  0  0  1  1  0  1  1  1   1   5
    5:  1  1  1  1  0  0  0  0  0   1   4
    6:  1  1  0  1  1  0  1  0  1   1   5
       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 ans
    1:  0  0  1  0  1  1  0  1  1   1   6
    2:  0  0  1  1  0  0  1  1  1   1   4
    3:  0  1  0  0  0  1  1  1  1   0   6
    4:  0  0  0  1  1  0  1  1  1   1   5
    5:  1  1  1  1  0  0  0  0  0   1   4
    6:  1  1  0  1  1  0  1  0  1   1   5
       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 ans
    1:  0  0  1  0  1  1  0  1  1   1   6
    2:  0  0  1  1  0  0  1  1  1   1   7
    3:  0  1  0  0  0  1  1  1  1   0   7
    4:  0  0  0  1  1  0  1  1  1   1   7
    5:  1  1  1  1  0  0  0  0  0   1   4
    6:  1  1  0  1  1  0  1  0  1   1   7
       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 ans
    1:  0  0  1  0  1  1  0  1  1   1   8
    2:  0  0  1  1  0  0  1  1  1   1   8
    3:  0  1  0  0  0  1  1  1  1   0   8
    4:  0  0  0  1  1  0  1  1  1   1   8
    5:  1  1  1  1  0  0  0  0  0   1   4
    6:  1  1  0  1  1  0  1  0  1   1   7
       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 ans
    1:  0  0  1  0  1  1  0  1  1   1   9
    2:  0  0  1  1  0  0  1  1  1   1   9
    3:  0  1  0  0  0  1  1  1  1   0   9
    4:  0  0  0  1  1  0  1  1  1   1   9
    5:  1  1  1  1  0  0  0  0  0   1   4
    6:  1  1  0  1  1  0  1  0  1   1   9
       V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 ans
    1:  0  0  1  0  1  1  0  1  1   1  10
    2:  0  0  1  1  0  0  1  1  1   1  10
    3:  0  1  0  0  0  1  1  1  1   0   9
    4:  0  0  0  1  1  0  1  1  1   1  10
    5:  1  1  1  1  0  0  0  0  0   1  10
    6:  1  1  0  1  1  0  1  0  1   1  10
    > 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:

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.