R Tutorial : Basic Data Types

R has four basic data types: numeric (decimals), character (text in quotes), logical (TRUE or FALSE), and NA (missing values). Whole numbers default to numeric unless you add L to force integer storage. Use class() to check what type a variable is.
Chapters
Now that you're familiar with how to run commands in R, let's get a bit more technical into exactly what types of data you have been manipulating. There are a number of basic atomic data types in R. Here are the most common ones. Numeric data are decimal numbers like 42.5. Numeric data could be a stock price, cash flow or income. A special type of numeric data is integers. Integers are whole numbers that do not have a decimal place. By default, whole numbers like 5 are stored as numeric data unless you specifically tell R that you wanted to store five as an integer. To do this, add a capital L after the number. You will almost always use numerics over integers in R, so this default behavior is nothing to worry about. Characters, also called strings, are text values such as hello world or 40. Character data could be a sequence of names or categories for your data, or even credit ratings for bonds. Note the quotation marks telling R that you are giving it character data. Even numbers can be stored as character data if you put it in quotation marks. And finally, logicals are the so called boolean values of true and false. R requires that true and false are capitalized, so this would cause an error technically. NA to denote a missing value is also illogical, but but we won't go too deep into that here. So far you have stored numeric data in variables, but you could have just as easily stored logical or character data in a variable as well. Here the logical true is assigned to myanswer and the word carrots is assigned to food. Given a variable, you can use R to determine what data type it is. Using class Using class on myanswer returns logical, telling you that the myanswer variable contains a logical data type. Class is what is known as a function. You will see a number of useful functions throughout the course, but for now you can think of them as a black box where you pass in some kind of input. The function calculates some value and presents it to you as output. You can use class to see the difference in type between a numeric and integer version of 5. Let's find out what you learned. Head over to the next exercise to try out your knowledge of data types.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







