简体   繁体   中英

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM