Ankita
Ankita

Reputation: 485

Equivalent of "tabulate" and "C"of R in python

I am currently working on transferring code available in R to python. In R, there is a function called tabulate and C.

Is there is any function available in python that is equivalent to tabulate and C function of R?

Upvotes: 0

Views: 587

Answers (1)

Dan
Dan

Reputation: 45752

From the docs for c it is used to

Combine Values into a Vector or List

In python you can combine values into a list using brackets so c(1, 2, 3) in R become [1, 2, 3] in Python. But if you didn't know this, I really urge you to stop porting the code and take two days to go through a basic Python tutorial. Understanding lists in Python is about as basic as you get.

The docs for tabulate state

tabulate takes the integer-valued vector bin and counts the number of times each integer occurs in it.

this sounds just like numpy's bincount

Count number of occurrences of each value in array of non-negative ints.

unless you have a requirement to also count negative ints? If not, it's pretty much a drop in replacement. So tabulate(c(2,3,3,5), nbins = 10) becomes np.bincount(np.array([2, 3, 3, 5]), minlength=10)

Upvotes: 2

Related Questions