Practicing Data Types

  1. Create a variable called my_name and assign your name to it.

  2. Check the class of my_name using the class() function.

  3. Now a weird one. Create a variable called ten and assign in the value "10" in quotes. What’s the class of ten? Why is it not numeric?

  4. What happens if you add my_age and ten?

  5. To convert ten to a numeric type, type ten = as.numeric(ten). Now check it’s class again.

  6. Now can you add my_age to ten?

Solutions

Here’s the deal with programming: the only way to learn to program is to wrestle with solving your own problems. The best way to learn is to do so actively – if you just look at answers as soon as you get stuck, your process is more passive. You may feel like you’re learning more, but research shows that that’s an illusion – students who learn passively think they’re learning more than they are (a summary of work in this area is here).

Moreover, in coding in particular, the process of debugging your code is a critical skill in and of itself, and the only way you will learn to do it is by literally spending hours working through your own problems. And once you’ve seen an answer, there’s no way to unsee it, so proceed only if you absolutely have to.

OK, if you still want to proceed, you can find solutions here.