Why you should use vapply in R

Why you should use vapply in R

R
In this post we'll cover the vapply function in R. vapply is generally lesser known than the more popular sapply, lapply, and apply functions. However, it is very useful when you know what data type you're expecting to apply a function to as it helps to prevent silent errors. Because of this, it can be more advisable to use vapply rather than sapply or lapply. See more R articles by clicking here Examples Let's take the following example. Here, we have a list of numeric vectors and we want to get the max value of each vector. That's simple enough - we can just use sapply and apply the max function for each vector. [code lang="R"] test <- list(a = c(1, 3, 5), b = c(2,4,6), c = c(9,8,7)) sapply(test,…
Read More
How to create a progress bar in Python

How to create a progress bar in Python

Python
Creating a progress bar is really useful if you have a long-running task and you'd like to know how far along you are. In this post we'll talk about two packages for creating progress bars in Python: tqdm and progressbar2. Creating a progress bar with Python's tqdm package Let's get started by installing tqdm. We can do that using pip: [code] pip install tqdm [/code] Once we have tqdm installed, it's straightforward to use. Essentially, we just need to wrap whatever object we're looping over (i.e. any object that is iterable) inside tqdm. Then, when the code runs, Python will print out a progress bar so that you can know how far along you are. [code lang="python"] from tqdm import tqdm result = 0 for i in tqdm(range(10000000)): result +=…
Read More