Some rules of thumb for new R users (According to myself):
If you are working with
data.frames, forget there is a function called
apply- whatever you do - don't use it. Especially with a margin of 1 (the only good usecase for this function is to operate over
matrixcolumns- margin of 2).
- Some good alternatives:
?rowSums/rowMeans/etc, the awesome
matrixStatspackages (for matrices),
?rowsumand many more
- Some good alternatives:
For loops are not bad- don't listen to anyone who says otherwise. They are bad only in certain cases:
- If you use them to iterate over rows.
- If you are performing unvectorized/inefficient operation within each iteration
- If you are writing a loop for something that is already vectorized
R is a vectorized language- meaning many operation were already written in C loops- so don't reinvent the wheel and write stuff in R loops if it was already written. With one exception- many of these functions work only with matrices. Hence, if you have a
data.frameyou should think twice if you want it to be converted to a
matrix(you may experience some unexpected consequences as a result), or can you avoid it.
Learn base R before you learn any fancy packages such as
dplyr. It is a nice package and all, but it was designed for very specific things. Many many operations could be done much more efficiently using base R.
Get familiar with R classes. Learn what is
factorand how to use it. Know the difference between a
matrix(a vector with a
dimattribute) and a
listof vectors). Learn how and when to work with
arrays. Know the difference between
integer. Read about floating points.
Learn how and when yo use
lapply/sapply/vapply- these could come useful many times
You must learn some
?S4groupGenericin order to discover which functions have
data.framemethods (a very useful to know).
?strptimevery carefully (note the
Sys.setlocale("LC_TIME", "C")part - could be a life saver).
Read the damn docs. R has awesome documentation- please use it. You won't find anything even nearly as good in any other language (I know of).
Like Barry Rowlingson once said: "This is all documented in TFM. Those who WTFM don't want to have to WTFM again on the mailing list. RTFM."
Member for 4 years, 3 months
4 profile views
Last seen Jul 1 '20 at 14:35
- Stack Overflow 87.4k 87.4k 1515 gold badges123123 silver badges181181 bronze badges
- Code Golf 531 531 33 silver badges1111 bronze badges
- Meta Stack Exchange 334 334 11 silver badge99 bronze badges
- Cross Validated 316 316 44 silver badges1313 bronze badges
- Area 51 151 151 22 bronze badges
- View network profile
Top network posts
- 143 Is the "*apply" family really not vectorized?
- 140 Convert row names into first column
- 77 Split data frame string column into multiple columns
- 69 Find complement of a data frame (anti - join)
- 52 How to fill a matrix with zero (0)
- 48 How to remove duplicated column names in R?
- 41 Getting the top values by group
- View more network posts →