Basic Syntax in R
R uses symbols to represent:
- math operations (
+
,-
,*
, and/
and follows the order of operations) - logical operators (
>
,>=
,<
,<=
,==
, and!=
) - joining logical operators (
&
,&&
,|
and||
) - object assignment (
<-
or=
)
The names of objects in R must follow 3 rules:
- No spaces
- No symbols other than
.
or_
- Names must begin with a letter
## Order of operations
25+7/4 #26.75
(25+7)/4 #8
## AND vs OR
5 > 3 && 3 > 4 #FALSE
5 > 3 || 3 > 4 #TRUE
Variable Types in R
All objects in R have a variable type. The most common are:
- numeric: the object is a number
- character: the object contains letters
- logical: the object is either
TRUE
orFALSE
NA
: the object is blank (categorized as logical, but may be part of a character or numeric vector)
We can use the class()
function on an object to check its variable type.
class(2000) #numeric
class("apple") #character
class(NA) #logical