Remembering R – Using Spaced Repetition to finally write code fluently

Reading Time: 17 Minutes (25 Minutes including Appendix)

Writing R slower than a snail

Let me give you the setting: it’s June, it’s really nice outside, and my friends are enjoying the lake near the University. I, on the other hand, am making so little progress on my MA thesis that I’m not at the lake and chilling. I am sitting in the library with the AC turned to freezing, which means that I need to wear a sweater even in Summer.

The reason I’m making exactly zero progress is simple. I can’t write R code. Not fluently, at least.

Doesn’t matter that I’ve been working as a research assistant for 4 years, doing data processing every week using R. Doesn’t matter at all.

I’m still googling the simplest things, hopping from one StackOverflow question to the next, asking myself why the heck I can’t remember basic R syntax. Everything I want to do takes forever, and I’m wondering if I’m just too dumb for this. At this rate I’m never going to finish my analysis in time.

Then two things happen shortly after another that thankfully end my suffering:

  1. I discover the Tidyverse and for the first time understand how my data flows. It just clicks, it’s amazing. But the Tidyverse has new grammar, new ways of doing things. And while it’s much easier to grasp, I’m still having a hard time remembering how to transform my data, how to clean it, using these new tools.
  2. I remember this one tool I used a while back that might help me: Anki.

Anki is a flashcard program that algorithmically determines the point in time where you’re most likely to forget something and tests you on it, which makes that fact stick longer in your memory. A while before, I had read this great review of the science behind spaced repetition, the concept behind Anki, at Gwern’s blog and even used it years before to try to learn Croatian (to admittedly samo malo success).

Unfortunately, using Anki itself never stuck, so I was never as successful using it as I could have been. But I knew how incredibly powerful it could be, so I spent two days setting up Anki again and putting every function on this Data Wrangling Cheatsheet into it.

And then…magic happened. Within a week, I wrote code from memory. I didn’t have to look up how to transform a data frame from wide to long format, how to select only certain rows or remove columns that I didn’t need.

I wrote R code like it was normal text! It was completely surreal. I still had to think about what I wanted to do with my code, how the data or the analysis should look like in the end. But I didn’t need to think about how to get there anymore.

Suddenly, I had the feeling of making progress. I still had a lot of work to do for my thesis, and I still spent a lot of time on StackOverflow. But I did so for solving real problems, not for figuring out how to do basic R things.

In this guide I’ll show you how you too can flip that switch and write R code like it’s nothing.

There have been written great essays about Spaced Repetition and the science behind it and how spaced repetition is the perfect way to augment your long term memory and I encourage you strongly to read them both. They might just change your life forever, just as they have changed mine, showing you that

  • remembering is a choice, that
  • you can control what you can recall at a moments notice.

The one thing these essays don’t talk about as much is implementing spaced repetition. So that’s what I want to do here. Show you that you are able to learn writing R code, and that you can learn to write R fast. The one thing this does require is consistency, but we’ll return to that a bit later.

So, how are you going to learn R using Anki and spaced repetition? In this guide I’ll show you

  1. how to set up Anki so that it’s super easy to add new code snippets to it,
  2. how to write good flash cards - especially for programming,
  3. how to make this a habit that makes you a badass R programmer for the rest of your life and
  4. if you fiddle and optimize a bit, how to configure it so you get the most out of the time you spend reviewing

If you like this guide and want to learn more about R, data processing and management and how to make your life dealing with data easier, also sign up for my email newsletter so I can let you know about new articles.

The Tool: Setting Up Anki

Before I can show you how to use spaced repetition in practice, you’ll first need to install the necessary tools. The basis for taking full advantage of Spaced Repetition is Anki. There are of course other programs that use slightly different algorithms to optimize your learning time, but Anki is incredibly powerful and has a free desktop version, so that is what I’m going to show you. Anki also has an extremely vibrant community that is helpful and friendly to beginners, which is another plus.

Installing Anki And Extensions

To install Anki, go to https://apps.ankiweb.net/ and click on “Download”. Choose Anki 2.1 and follow the installation instructions.
When you open up Anki, this is what you should see:

What you should see when you open Anki the first time

Figure 1: What you should see when you open Anki the first time

(I am using the macOS version of Anki, so yours will look slightly different if you’re on Windows or Linux, but it will work the same.)

With Anki installed, you will need to install two other things, two Anki Add-ons:

  1. Mini Format Pack
  2. Syntax Highlighting for Code

To install Add-ons, go to “Tools” in the Anki Menu and then go to the “Add-ons” menu:

In the window that opens, click on “Get Add-ons”:

In this window, you will need to enter the numeric IDs of the addons you want to install. To get these IDs you either click on “Browse Add-ons”, which will take you here, and then search manually for the two extensions we want to install, or you follow these two links which will take you to the Add-on pages directly:

On these pages, you will find a section “Download” with a number code that looks something like the picture on the left, which you will need to paste into the “Code” field of the “Get Add-on” window:

Click on “OK” and repeat the process to install the second Add-on.
Once you have both Add-ons installed, you’ll need to tweak the settings in two places. First, in the Add-ons overview window, select the “Syntax Highlighting for Code” Add-on and click the “Config” button:

In the window that opens, adjust the section that reads “limitToLangs”. Depending on which programming languages you want to learn, provide their names as a comma-separated list inside the brackets, with each language enclosed in quotes. Note: For R to be recognized, the language you have to give is actually S for historical reasons. So you would have something like:

"limitToLangs": [
        "S",
        "Python",
        "Bash"
    ],

if you wanted to learn R, Python and how to use the command line. Adjust the “style” section to read "style": "manni" and click on “OK”.

Back in the “Tools” menu, find the “Syntax Highlighting Options” menu and adjust the setting there to have “Line Numbers” unchecked, “Center Code Fragments” unchecked, “Use CSS classes” checked and “Default to last language used per deck” checked, like this:

You have now installed everything to start learning R and memorizing its syntax with Anki. Now we can move on to the nitty gritty.

The Skill: How To Make Good Flashcards To Learn Programming

To help you make the most out of your time using Anki and to really write R code fluently I want to share a couple of tips and strategies with you that have helped me tremendously.

The first one is to trust the system.

Spaced Repetition and Anki work, really work, if you follow the system. It’s been scientifically proven, and even though you might think “this is just rote memorization, this is bad”, it is not.

Zed Shaw, one of my personal heroes, wrote a great essay about this, pointing out that we learn writing itself this way and that even artists spend hours repeating the basics over and over again until they are logged into their brains and even long after. So again, trust this system, I promise you it works.

Spaced Repetition also has a large and active community devoted to it, and one of the corner stones of this community is an essay called Effective learning: Twenty rules of formulating knowledge by Dr. Peter Wozniak, the inventor of SuperMemo. If you are interested in applying Spaced Repetition and Anki to more than learning R, I highly recommend you read it (and maybe even make flashcards about it :haha:).

While I don’t want to rehash all of these rules, I do want to apply some of them directly to learning R to show you how to make good flashcards yourself on your journey.

Rules 1 and 2: “Do not learn if you do not understand” and “Learn before you memorize”. This is particularly important when learning programming. You need a good understanding of what the code, the functions, and the rules you are learning do and are used for. In theory, you could go through the whole R Documentation and make a flashcard for every function or behaviour, commit that to memory using Anki and then “know” how to write R. And that would definitely work! But you would have two problems: you’d have a miserable time doing it and you’d still not know when to apply it and where. Anki is a tool for remembering things you haved learned, not for learning things you’ve never seen before.

Rule 3 and 4: “Build upon the basics” and “Stick to the minimum information principle”. You do not need to have read and understood all of Hadley Wickhams “Advanced R” before you start memorizing things.

On the contrary, start memorizing tiny facts that you have understood. Make the most basic behaviors of R so automatic that you forget that you ever needed to learn them.

Rule 5: “Cloze deletion is easy and effective”. Cloze deletion is when you are shown a sentence with a part missing and replaced by dots, which you then have to remember. Cloze deletion is essential for learning R.

By looking at code and identifying what’s missing, you build an essential skill for a programmer and memorize code in its actual real-world context where you want to use it.

I also want to share two rules with you that I’ve figured out on my own that apply directly to programming flashcards.

1. You have to type your answers. Anki is great in that it not only lets you use cloze deletions, but also provides the option to type the answer to the clozes. This is critical for any programming-related memorization.

A lot of what you want to train is in essence muscle memory. You come across a task or problem and your fingers just move to type the solution. If you use cloze deletions to only see the answer instead of typing it you are not doing that, and with code in particular it’s super easy to cheat yourself unconsciously by thinking “yeah, yeah, I knew that, next”.

Typing the clozes makes that harder because you get direct visual feedback of where you missed even a single character, which is critical when writing code.

2. Give an explicit task and provide all necessary information.

In the beginning, I often asked myself general questions, like “How does assignment work in R?” and then had an answer on the back of the card. That’s bad, because it doesn’t reflect how you’re going to use your knowledge when programming.

When you’re programming, you’ll need to actually assign some value or data frame or whatever, not explain how assignment works1.

So, make cards that ask you to do a concrete task with a concrete solution. In fact, I think it’s best if I demonstrate this directly and show you how to create new cards in Anki.

Example - How to Create New Flashcards for R in Anki

I’ve created a sample collection of cards, including a custom card type for you to reuse with your own cards, that you can download here. If you’d rather not download the example deck but want to set up Anki completely yourself, check out this section where I show you how to configure Anki cards yourself.

Once you have downloaded the deck, go to File > Import and then select the Empiria.io – Demo Deck.apkg file you got out of the .zip-File you downloaded. Anki should now look like this:

To start making cards, click on “Create Deck” (more on decks and cards here) and give it a name. Then click on that deck and then click on “Add”. You’ll see something like this:

From the drop-down menu that says Basic, select the note type Programming - Typing:

Now let’s make two Anki cards. In the first one we are going to start really early and train how to assign a data frame, and in the second one we are jumping into the Tidyverse to learn how to make a new column.

I’m going to do this in a list of steps, which you can simply repeat for every card you make on your own.

  1. Think of a concrete task that you want to practice
    Write this task into the “Task” field of your new Card:
    “Assign the number 10 to the variable x
  2. Write the code for the example, either in RStudio or any other text editor
    x <- 10
  3. Make sure “S” is selected in the box next to the Lightning symbol

  1. Copy the code
  2. Paste with Alt + S
    Select the “Code” field with your cursor and press Option/Alt + S. This will add your code, formatted correctly. Do not use your normal paste-command, use Option/Alt - S.

  1. Select portion you want to memorize Select the code you want to memorize, which here is everything. Make sure to select no whitespace at the beginning or the end, this might mess up formatting.

  1. Press Shift-Cmd-C to turn into a cloze Press Shift-Cmd-C to turn the selected code into a cloze. When you want to create multiple clozes for one piece of code, repeat this for the other parts. Note that clozes can’t overlap.

  1. If there is anything going on that you want to explain, type this explanation into the “Explain” field.
  2. Write “R” into the “Category” field (unless it’s already in there)
  3. Click on “Cards” to see a preview and check if everything is looking as expected.

  1. Click on add. Done.

You now have your first flashcard for learning R, good.

Let’s repeat that with another example, with slightly more complex code.

  1. Think of a concrete task that you want to practice
    Write this task into the “Task” field of your new Card:
    “Take the data frame df and use the pipe to create a new column called sum that is calculated from the columns a and b.”
  2. Write the code for the example, either in RStudio or any other text editor
    with_sum = df %>% mutate(sum = a + b)
  3. Make sure “S” is selected in the box next to the Lightning symbol
  4. Copy the code
  5. Paste with Alt-S Select the “Code” field with your cursor and press Alt-S. This will add your code, formatted correctly. Do not use your normal paste-command, use Alt-S.
  6. Select portion you want to memorize
    Select the code you want to memorize. Here, select %>% and do step 7, then select mutate and do step 7, and finally select sum = a + b and do step 7. Make sure to select no whitespace at the beginning or the end, this might mess up formatting.
  7. Press Shift-Cmd-C to turn into a cloze
    Press Shift-Cmd-C to turn the selected code into a cloze. When you want to create multiple clozes for one piece of code, repeat this for the other parts. Note that clozes can’t overlap.
  8. If there is anything going on that you want to explain, type this explanation into the “Explain” field.
  9. Write “R” into the “Category” field (unless it’s already in there)
  10. Click on “Cards” to see a preview and check if everything is looking as expected.
  11. Click on add. Done.

The Practice: Sticking To Your Ankis

You know what Spaced Repetition and Anki are, you have set up Anki properly and made your first flashcards, so you are now set for finally remembering R syntax for good. Let me give you a couple of final pointers to guide you on your way.

Using Anki can be your secret weapon, the magical tool in your belt to not only learn to finally be fluent in R, but to learn literally anything.

There is only one thing you have to do: do your Ankis every day.

Unless you’re so sick you can’t read or someone dies, do your Ankis. I know it sounds over the top to say it like this, but I myself have made the mistake of letting my Ankis slip too often. And then I came back to reviews in the 1000s (no joke) and threw away my cards, starting from scratch because I was so discouraged.

Don’t suffer like I did. It takes ~10-20min per day, depending on how much knowledge you choose to put into it. Start slow and steady, and explore these steps to see if they make it easier for you:

  1. Opening the app and doing one single card counts. The most important thing is to establish the habit of using Anki, and there’s some great research on using tiny actions to build a habit. I also recommend reading books on building habits in general (Duhigg, Clear).

  2. Do Ankis at a fixed time of day. I do mine directly after getting up and dressing. That way no matter how my day goes I have accomplished something and nothing can come in the way of me doing my Ankis. Some people suggest doing Ankis directly before sleep, and if that works for you, go for it. I know that I’m way less disciplined in the evening compared to the morning, so I do them in the morning. Know thyself.

  3. Keep a Lights Spreadsheet or use other habit-supporting apps (something like Strides) to help you visualize your progress in doing Ankis. I find it incredibly motivating to know I have done Ankis 20, 30 days in a row.

  4. Do not learn too many new cards per day. The default of 20 new cards per day is good, maybe do 30 if you’re confident that you are sticking with it. Do not give in to the hubris of plowing through 100 cards a day and then stopping to use Anki because you suddenly have to do 500, 600 or even more reviews per day. Anki is a slow and steady game. Be humble here so you can be a badass later.

More Resources

Anki is powerful, and has an active community that is very helpful. And I am certainly not the first to have written about using Anki to learn to code. I’ve collected a couple of links that might be useful for diving deeper into Anki-Land:

  1. Anki Manual: The official manual for Anki. Can be intimidating and a bit dense, but is still invaluable.
  2. Guide to Anki Intervals and Learning Steps: A deep-dive into how the scheduling of cards works in Anki
  3. What Anki Learning Steps To Use The articles I’m working off of
  4. Anki Subreddit. A particular shoutout to /u/Glutanimate, the author of the two plugins we are using. Phenomenal work, wouldn’t work without his contributions.
  5. Michael Nielsen on Augmenting Long Term Memory: Incredible essay on working on and with your long term memory. Can’t wait for the essay on general knowledge management.
  6. Gwern’s Essay on Spaced Repetition: Science, data, everything you need to know about Spaced Repetition and why it works.
  7. My Anki Demo-Deck: If you don’t want to create your own cards template to get started, here’s mine.
  8. Janki Method: Using Anki to learn programming in general. Super interesting and closely related to this article.
  9. Derek Sivers on Anki
  10. RStudio Cheat Sheets: Invaluable if you want to write readable R, fast.
  11. Charles Duhigg - Power of Habit: Great book on creating and maintaining new habits.
  12. James Clear - Atomic Habits: The book for implementing habits in your life

Appendix

Setting Up Flash Cards in Anki

Anki is an extremely powerful tool for learning just about anything because it allows you to create custom formatted flashcards that look and behave exactly like you want them to behave. However, with great power comes great complexity: setting up Anki to your liking can quickly become a hobby in itself and beginners in particular are often easily overwhelmed when starting to use Anki. In the following, I’ll show you how to create the ideal card type for learning R and any other programming language. If this seems intimidating to you or you just want to get on with your life and learn R already (very sensible, get after it!), no worries, I’ve created a sample collection of cards, including a custom card type for you to reuse with your own cards, that you can download here and once you’ve done that you can skip to making your cards.

If you have time on your hands and want to learn how to optimize Anki: let’s go.

Back in the main Anki window, click on “Add”. This will open a new window, in which you will enter the information for your cards. Here’s a quick overview:

  1. Is the card type (here: “Basic”), which determines which fields a card has and how it behaves. We will make a new one that is taylored to our needs.
  2. This is the Deck, the pile of cards we want to add our new cards to. This can stay the way it is.
  3. Here we customize which fields our card has. Currently we have the “Front” and “Back” fields to which we can add information that Anki uses to create the cards.
  4. This lets us edit the cards that Anki shows us, such as how they look and how they behave.
  5. Here we select for which programming language we want to have syntax highlighting.
  6. This field will let us add cloze deletions, the power feature for learning programming and your new secret weapon. You’ll learn more about how exactly this works in the next section.

To make our own custom cards to learn R, we first need to add and rename some of the fields. Because we want to leave the type “Basic” intact, we will make a copy of the type and then edit this copy. To do so, click on Type: “Basic”, then on “Manage”, “Add”, select “Clone: Basic”, confirm by clicking “OK” and then give the new card type you want to create a name. I chose “Programming - Typing” as a name. Confirm by clicking “OK”, close the pop-up windows and select your new note type:

Now, click on “Fields…” and then rename “Front” to “Task” and “Back” to “Code”:

Then, click on “Add” and add two new fields. One called “Explanation” and one called “Category”. For the “Category” field, make sure to select “remember last input”.

Then confirm by clicking “Close”. Then click on the “Cards” button, which will take you to a window looking something like this:

  • Front Template (1) shows you the HTML of the front side of the card
  • Styling (2) shows you the CSS code for the highlighting
  • Back Template (3) Shows you the HTML for the back side of the card

In the front and back template you see things like {{Task}}, which signify the fields you created. You can reference any field of your card in your template by enclosing it in {{}}.

To set your new card type up for learning R (or any programming language), copy the following code into the Front Template:

Copy the following code into the Front Template:

<b>{{Category}}:</b><br>
<hr>
{{Task}}<br>

<div class='code'>
{{cloze:Code}}
</div>
 
{{type:cloze:Code}}

If you know HTML this code is obvious to you, if you don’t know HTML do not worry, you do not need to. Just make sure you have copied everything into the field and then copy the following code into the Back Template:

<b>{{Category}}</b><br>
<hr>

{{Task}}<br>

<div class='code'>
{{cloze:Code}}</div><br>
<div style='text-align: center;'>{{type:cloze:Code}}</div>
<br>
<div style='font-family: Helvetica; font-size: 20px;'>{{Explanation}}</div>

The code for the Styling field is very long, so here’s a link to the styling text file. Simply copy and paste its content into the appropriate field. This will ensure that you have proper highlighting of the different variables and functions when Anki shows you code.

Once you have done all that, you are now set to make flash cards.

Decks, Cards, and Settings

Let’s talk a bit about some very basic terms in Anki parlance and then configure Anki to maximize what you get out of your time using it. I know that this seems incredibly complex, and it is. If it’s too annoying to you, you can download a sample collection of cards, including a custom card type for you to reuse with your own cards and once you’ve done that you can go make your own cards and just start to learn. It’s more important that you start using Anki as soon as possible than read through everything I’ve written.

In Anki, a deck is a stack of flashcards that belong together. When you opened up Anki for the first time, you had one deck already, called Default. You are not limited to this one deck, you can create multiple (many) decks for different purposes, so one for R, one for learning Spanish and one for learning Geography. But it’s generally not recommended to have too many decks, and I would indeed recommend that you stick with one single deck for the time being, even if you use Anki for more than learning R. That’s because doing your reviews becomes a lot less monotonous and boring if you are getting asked about more than just a single topic, which helps you stick with doing your Ankis and therefore helps you learn better.

Anki will show you the cards you create according to how well you remembered them the last time. To do that, Anki divides your cards into three stages: cards you are just learning, cards you have learned and are reviewing and cards you had learned but recently didn’t remember correctly. I’m not going to go into detail here about how Ankis algorithm works exactly, just so much: every time you are shown a card, you tell Anki whether you know the answer and if yes how well.

For example, in this card I can answer whether I didn’t know it at all, if it was hard to remember, I remembered it or if it was really easy to remember. Depending on how I answer, the interval when I will next see the card changes: either in 30 minutes, 2 days, 3 days or 4 days. How long these steps are differes depending on which stage of learning your card is in. Ankis default settings for the three stages (learning, review, lapsed) are not entirely optimal, so we are going to tweak them. Remember that you can always return these settings to the default if you are not happy with them or tweak them to your own liking. This is just a best practice to get you started.

On your main Anki screen, click on the Gear icon next to the Default deck and select Options. You are now in the options tab that edits the behavior for new cards, cards you are just learning. Change the settings to the following:

In the Learning Tab:

  • Steps (in Minutes): 10 1440 8640
    Anki gives you multiple steps to learn a card. Here, we set the steps to 10 minutes, one day (1440 minutes) and 6 days. So if you know a card once, you will see it again in 10 minutes, if you know it again in one day and if you then know it again in six days.
  • Order: Show new cards in random order This way you are not cheating yourself because you are helped in remembering the content of a card because you know its context in the deck, like the card that came before.
  • New cards per day:
    You can adjust this to your liking. I personally have it set to 30. Remember that the higher you set this, the more reviews you will have tomorrow and the days after in addition to the new cards you are seeing for the first time. The Anki Manual states that with a limit of 20 you’ll on average have to review 200 cards per day. Act accordingly.
  • Graduating Interval: 15 days
    This means that if you know a card after having it not seen for 15 days it will move from the “learning” stage to the “review” stage. The purpose of increasing this so much is so that your have truly adequate time for learning a card before Anki penalizes you for forgetting it once.
  • Easy Interval: 20 days
    If you press easy when reviewing a card, Anki immediately moves it to the Review stage and skips any learning steps.
  • check the box at Bury related cards Another option that helps you learn better by removing the “context” of a card.

In the Reviews Tab:

  • Maximum reviews per day: 9999 You want to do all reviews that Anki tells you to do, because otherwise you are messing up the algorithm. The best way to limit how many reviews you have to do per day is to only learn a low number of new cards per day and to do your Ankis literally every day so that you never have a backlog of reviews to come back to.
  • check the box at Bury related reviews
    Again, remove context to remember you cards better on their own.

In the Lapses Tab:

  • Steps (in Minutes): 30 1440
    The steps for re-learning cards where you lapsed once.
  • New Interval: 20%
    How soon after remembering a lapsed card you want to see it again? By default Anki has 0, effectively turning a lapsed card into a totally new card. But after once forgetting a card you knew for a year, do you really need to see it tomorrow, the day after and a hundred times more? No, so 20% of the previous interval is good.

If you have any feedback or question, do not hesitate to email me! You can also use the anonymous feedback form if you prefer.


  1. Obviously, if you do think you’re going to get asked such a question, maybe for an exam or an interview, make a card that addresses that! Refer to Rule 17: Redundancy does not contradict minimum information principle.