Coursera R Mentoring
Mentoring records and my helping articles on Coursera (Last update: July 29, 2019)
Some questions have been raised frequently in the Coursera R Programming course forum. Here I organized those questions and collected my replies to them.
Please note that these articles are not intended to be self-sufficient resources for learning R. If you are interested in R language, here are some good starts:
- Data Science Specialization of JHU on Coursera (free auditing)
- swirl package of R (R and swirl installation required)
- R語言翻轉教室 (only for Mandarin-speaking users)
Besides, RStudio and R users around the world have contributed many cheatsheets which can be referred to anytime. Check RStudio Cheat Sheets for them.
For advanced R users. If you are interested in more detailed mechanisms of R grammar, you may refer to Advanced R written by Hadley Wickham, and Advanced R Programming.
BTW, for those who would like to take courses from Coursera but cannot afford the cost, I have some suggestions (written in Mandarin).
Getting started
- Why R but not Excel: Here I listed some advantages of R over Excel from my point of view.
- Use R or RStudio: RStudio can’t be executed without R. Still they have different advantages.
- Chinese user names cause errors: If your computer user name comprise Chinese characters and you get errors when running RStudio, please refer to this article.
- Use
<-or=: Using either<-or=to assign values in R is a question related to personal preferences. I shared my own opinions here. - Why
strptimereturns NAs: Languages from different system locales may cause some abnormalities. However we can change the locale settings within R scope without changing the whole OS.
Data types
- “Vectors” in R: Vectors in R don’t have mathematical sense. Yet “mathematical vectors” and matrix operations can still be performed.
- Number types: R has class “integer” and “numeric”, they are different in memory management and capability to be manipulated.
Data preprocessing
- Difference between
[[]]and$: There are various extract operators in R. In this short paragragh I explain the difference between[[]]and$, and provide a link to an even more comprehensive article written by another Community Mentor. - Factor levels and labels: Categorical data are called “factor” in R. This article explains what are factor levels and factor labels respectively.
- Split data: Data can be splited with specified categories. However the length of the factor variables matters.
- Paste strings: Pasting various strings together is a plausible way to create variables. Here I introduce the two critical arguments, sep and collapse, of
pastefunction in R. - Argument MARGIN for
apply:applyallows users to execute a function over dimensions of matrices or arrays. What if we assign multiple dimensions at a time?
Comparison and ranking of data
- Difference between
==and%in%: Both==and%in%are often used to find matches in data. Yet, they have one difference when we have multiple inputs to be compared in parallel with the target. - Order, rank, and sort: These three functions are commonly used to make data into orders, but they are confusing with one another. This article tries to offer clarifications.
Functional programming
- Difference between
printandreturnfrom a function: Bothprintandreturnmake objects “visible” outside the executed function, but onlyreturntruly returns values that can be saved for later use. Examples are provided in this article.
Lexical scoping of R language
- Lexical scoping: This article demonstrates what would happen if an object is defined not only outside but also inside the function.
- Constructor functions: With knowledge of lexical scoping, we are able to write a function which creates other functions.
- Optimizing parameters: Another example showing how lexical scoping can help you optimize your interested parameters.
Generic functions
- How
meanwas created: The source codes ofmeancannot be seen by simply typingmeanin the console. This article demonstrates how to see them.