Basically, if your code prints too much output to the console, it will slow down RStudio and your R code as well. It may be due to all the output clogging up the memory in RStudio. R is known to be “single thread” so it can only use 1 CPU at a time, even if your computer has multiple cores.
Hence, the tips are to:
Reduce the number of print statements in the code manually.
Set quiet=TRUE in all scan statements. Basically, the default behavior is that scan() will print a line, saying how many items have been read.
This is especially true with for loops, since the amount of printed output can easily number to the millions, and overwhelm RStudio.
Most of the time, users of R and Python will rely on packages and libraries as far as possible, in order to avoid “reinventing the wheel”. Packages that are established are also often superior and preferred, due to lower chance of errors and bugs.
We list down the most popular and useful packages in R and Python for data science, statistics, and machine learning.
In the R language, often you have to convert variables to “factor” or “categorical”. There is a known issue in the ‘caret’ library that may cause errors when you do that in a certain way.
The correct way to convert variables to ‘factor’ is:
trainset$Churn = as.factor(trainset$Churn)
In particular, “the train() function in caret does not handle factor variables well” when you convert to factors using other methods.
Basically, if you use other ways to convert to ‘factor’, the code may still run, but there may be some ‘weird’ issues that leads to inaccurate predictions (for instance if you are doing logistic regression, decision trees, etc.)
R has the package “psych” which allows one to calculate the Cronbach’s alpha very easily just by one line:
For Python, the situation is more tricky since there does not seem to exist any package for calculating Cronbach’s alpha. Fortunately, the formula is not very complicated and it can be calculated in a few lines.
An existing code can be found on StackOverflow, but it has some small “bugs”. The corrected version is: