2

Is there a faster way to search for indices rather than which %in% R.

I am having a statement which I need to execute but its taking a lot of time.

statement:

total_authors<-paper_author$author_id[which(paper_author$paper_id%in%paper_author$paper_id[which(paper_author$author_id%in%data_authors[i])])]

How can this be done in a faster manner?

2
  • Are you sure that this indexing is the slow part of your code? Commented Apr 8, 2014 at 11:11
  • my code is very running very slow and it mainly uses which statements so i thought that it could be due to which statements Commented Apr 8, 2014 at 11:25

1 Answer 1

1

Don't call which. R accepts logical vectors as indices, so the call is superfluous. In light of sgibb's comment, you can keep which if you are sure that you will also get at least one match. (If there are no matches, then which returns an empty vector and you get everything instead of nothing. See Unexpected behavior using -which() in R when the search term is not found.)

Secondly, the code looks a little cleaner if you use with.

Thirdly, I think you want a single index with & rather than a double index.

total_authors <- with(
  paper_author,
  author_id[paper_id %in% paper_id & author_id %in% data_authors[i]
)
Sign up to request clarification or add additional context in comments.

2 Comments

Don't call which is surprisingly not faster than which: stackoverflow.com/questions/17510778/…
@sgibb it's dangerous to generalised from that one case, and general you should avoid performance optimisation in favour of clear code (unless it's obvious that they reduce a bottleneck)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.