Basic skills in R: Your first calculations with R
Data types
- Numeric data type: These are numbers, possibly in scientific notation. They can be
- integers (\(\ldots, -2,-1,0,1,2,\ldots\));
- floating-point numbers, i.e., numbers with a decimal point such as \(1.23\), \(1.234\mathrm{e}56\) or \(1.234\mathrm{E}56\);
- complex numbers such as \(2+3\ii\).
- Character data type: This all about letters, words. and other characters. A sequence of characters is also called a string. Remember to enclose the characters by quotes. For example "fifteen" or "child's age".
- Logical data: This concerns binary choices, like "yes" or "no," "on" or "off", TRUE, FALSE. We'll see in another theory page why on earth you would like to store "yesses" and "no's" in a variable. For the moment it suffices to consider the logical data type has one of two possible values: true or false, which is intended to represent the two truth values used in logic.
- Factor data: This is not really a basic data type, but more a data structure. Nevertheless we put it in this list as it can be considered as a type used for categorical data (for example, factors for a book object could be the genre, the colour of the cover, the title, the name of the author, the name of the publisher, and so on). See the page Working with factors if you want to learn more about this type.
- Date: This is not really a basic data type either, but more a distinguished use of an integer. Dates are represented as the number of days since the 1st of Januay 1970, with negative values for earlier dates.
Why do we need to know about data types? Knowing the data type of your variables is important because it helps you perform the right operations on them. For example, you can add numbers, concatenate strings, or compare values, but you can't add a string to a number. Knowing the data types allows you to use the appropriate operators and prevent errors.
Explanation Let's look at some examples of how to create variables of various data types and check their properties. Don't worry about no further discussion of the logical data type or factor yet. At this point, the goal is only to let you know that there are multiple data types and that two of them are called factor and logical. You will learn more about them later. |
Sample script # create a numeric variable
|
Explanation
R assigns data types automatically to variables, as we saw above. But R has several functions to convert variables from one data type to another. This type of conversion is called coercion. Here are some commonly used functions:as.character()
: converts an object to character type.as.numeric()
: converts an object to numeric type.as.logical()
: converts an object to logical type.
Try some of them out!
Sample script
# convert a numerical variable to character
x <- 5
x <- as.character(x)
str(x)
# output: chr "5"
# convert logical to numeric
v <- TRUE
v <- as.numeric(v)
str(v)
# output: num 1
Explanation
R has a special data type (or actually a class of object) to represent dates. The current date can be found by the function Sys.Date()
. The function format()
allows various choice of formatting the printing of a date. The functions weekdays()
and months()
return the name of the weekday and the month, respectively, for a given date.
An integer can be converted to a date via the coercion function as.Date()
and the origin
option allows you to choose a reference date.
See the sample session on the right-hand side
Sample session
> today <- Sys.Date() > typeof(today) [1] "double" > class(today) [1] "Date" > str(today) Date[1:1], format: "2023-09-22"
> format(today, "%d %b %y") [1] "22 Sep 23" > weekdays(today) [1] "Friday"
> two_weeks_later <- as.Date(14,
+ origin = today)
> two_weeks_later
[1] "2023-10-06"
> format(two_weeks_later,
+ "%d %b %y")
[1] "06 Oct 23"
Summary of logical test and coercion functions
Type | Logical test |
Coercion function |
numeric | is.numeric() |
as.numeric() |
complex | is.complex() |
as.complex() |
character | is.character() |
as.character() |
logical | is.logical() |
as.logical() |
factor | is.factor() |
as.factor() |