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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:03:02+00:00 2026-06-06T11:03:02+00:00

In R, is it possible to perform immediate logical indexing on a function’s output?

  • 0

In R, is it possible to perform “immediate” logical indexing on a function’s output?

To clarify this somewhat obscure question, here’s a little everyday example the like of which I am sure many people have come across before. Suppose we have a vector “data”, like the following:

data <- c(1,1,3,5,6,6,8,10,14,15,15,20)

If we now apply the function “tabulate” to this vector, the result will be:

tabulate(data)
 [1] 2 0 1 0 1 2 0 1 0 1 0 0 0 1 2 0 0 0 0 1

However, it is often desirable to access only those entries of the vector which are (in this case) non-zero, which would traditionally be done like so (I guess…):

tabulate(data)[tabulate(data) != 0]
[1] 2 1 1 2 1 1 1 2 1

However, in this case the “tabulate(data) would need to be calculated twice, which appears inefficient or even wasteful; at least, it is definitely not elegant. Likewise, storing the result of “tabulate(data) in a temporary variable can be cumbersome if one works with large datasets.

My question now simply is: does a simple, more elegant (syntactic) workaround for these kind of problems exist? Something like a “magic” direct.index function that does the job? Like so,

direct.index(tabulate.data, condition='!= 0')

…which would basically discard all values that do not meet the indexing condition already at the time of computation, making the whole process faster and more efficient.

The concrete problem with zero-removal from “tabulate” results is given here for simplicity; in fact, I’ve scratched my head about this in very many different situations. Maybe I also just have some basic misconception about R…

By the way, I’ve looked into “?subset”, but that does not seem to be what I’m looking for.

  • 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-06T11:03:05+00:00Added an answer on June 6, 2026 at 11:03 am

    A version of the function written by hand

    direct.index <- function(x, condition, value){
      index <- eval(call(condition, x, value))
      x[which(index)]
    }
    
    direct.index(tabulate(data), '!=', 0)
    ## [1] 2 1 1 2 1 1 1 2 1
    

    But, I think you are looking for the function table

    table(data)
    ## data
    ## 1  3  5  6  8 10 14 15 20 
    ## 2  1  1  2  1  1  1  2  1 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Convention question: When do you use a Getter/Setter function rather than using
Is it possible to perform multi-variate regression in Python using NumPy? The documentation here
EDIT: Just to clarify, my main question boils down to this: if you issue
Is it possible to advice the kernel to perform an immediate context switch to
Is it possible to perform a partial or patch commit in Team Foundation? I
Is it possible to perform query expansion with Lucene 3.6 using any ontology? I've
Is it possible to perform a replace-string operation inside of a rectangular region in
is it possible to perform a first commit on a new repository using a
Is it possible to perform a global reversed-find on NHibernate-managed objects? Specifically, I have
Is it possible to perform some custom processing when Windsor instantiates a type? Something

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.