2.3 Data types
In common with many programming languages, every variable in R is assigned a data type. The primary data types in R are Numeric
, Integer
, Character
, Logical
, and data structures, which will be covered in Section . The type of a variable determines what functions can be applied to it.
2.3.1 Numeric and Integer
Numerics in R store decimal values. Integers are a separate type to numerics and contain whole number values only. By default R will store numbers as numeric
, since most mathematical operations will require decimal point numbers (such as division).
In the previous section we declared a variable called x
, to which we assigned the value 42. R recognised this as a number and automatically set the type of x
to be numeric.
The following table shows the types of numeric and integer operations that can be performed:
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | x + 2 |
44 |
- | Subtraction | x-20 |
22 |
* | Multiplication | x*2.23 |
93.66 |
/ | Division | x/3.5 |
12 |
%/% | Integer division | x %% 3.5 |
0 |
^ | Exponential | x^2 |
1764 |
%% | Modulus | x%%2 |
0 |
In addition to the basic operations, there are more complex numeric functions, the following table shows some common examples.
Giveny <- 423.2332
and z <- -2.34
.
2.3.2 Character
A character variable is used to represent strings or text values in R. Characters are identified by the surrounding double (" “)or single quotes (‘’).
h <- "Hello world"
print(h)
## [1] "Hello world"
The following table shows some types of character operations that can be performed. Given the example h <- "Hello world"
.
Operator | Description | Example | Result |
---|---|---|---|
nchar(h) |
number of characters in variable \(x\) | 11 | |
substr(x,start,stop) |
extract substring from start position to stop position | substr(h,2,4) |
ell |
grep(pattern,x) |
search for \(pattern\) in variable \(x\) | grep('ell',h) |
1 |
grepl() |
provides a logical response as to whether the pattern exists. | TRUE | |
sub(pattern,rep,x) |
search for \(pattern\) and replace with \(rep\) in variable \(x\) | sub('ello','i',h) |
Hi world |
strsplit(x,delim) |
split the elements of a character variable \(x\) using the specified \(delim\) | strsplit(h,'o') |
c(“Hell”, " w“,”rld“) |
paste(...,sep) |
concatenate list of strings together (…) separating them using the \(sep\) delimiter | paste('a','b,'c',sep=',') |
a,b,c |
toupper(x) |
change to uppercase | toupper(h) |
HELLO WORLD |
tolower(y) |
chagne to lowercase | tolower(h) |
hello world |
Some examples:
hello <- paste("Hello", "World", sep="~")
print(hello)
nchar(hello)
substr(hello, 3, 7)
strsplit(hello,'~')
## [1] "Hello~World"
## [1] 11
## [1] "llo~W"
## [[1]]
## [1] "Hello" "World"
gsub("World", "R", hello)
grep("Hello", hello)
grep("Mouse", hello)
grepl("Stephen", hello)
## [1] "Hello~R"
## [1] 1
## integer(0)
## [1] FALSE
2.3.3 Logical
Logical datatypes can have one of only two values: TRUE
or FALSE
. Note, these are case-specific, True
or true
are not valid logical values. However, the shortcut: T
and `F
will work.
x <- 42
is.even <- x %% 2
is.even
## [1] 0
Setting, retrieving and changing data types
-
Enter the examples from the sections above (numerics to logical) in RStudio. Either into the console or the editor window.
- Remember, you can use the Environments tab to keep track of the variables being created.
- Note, below all the outputs are shown altogether for readability. If you are running them in console, you will see the output immediately as you hit [Enter].
-
Experiment with different values so you understand better how different operations work.