<- 5
variable_1 variable_1
[1] 5
Part 2: Variables
William Okech
June 22, 2022
Variables are instrumental in programming because they are used as “containers” to store data values.
To assign a value to a variable, we can use <−
or =
. However, most R users prefer to use <−
.
<-
=
->
The output of the variable can then be obtained by:
print(variable)
, andView(variable)
.Both print()
and View()
are some of the many built-in functions1 available in R.
In RStudio, the list of variables that have been loaded can be viewed in the environment pane.
Figure 1: A screenshot of the environment pane with the stored variables.
Output of View()
will be seen in the script pane
assign()
and rm()
functionsIn addition to using the assignment operators (<-
and =
), we can use the assign()
function to assign a value to a variable.
To remove the assignment of the value to the variable, either delete the variable in the “environment pane” or use the rm()
function.
After running rm()
look at the environment pane to confirm whether variable_7
has been removed.
At this point, you may be wondering what conventions are used for naming variables. First, variables need to have meaningful names such as current_temp, time_24_hr, or weight_lbs. However, we need to be mindful of the variable style guide which provides us with the appropriate rules for naming variables.
Some rules to keep in mind are:
variable
is not the same as Variable
),TRUE
, FALSE
, if
, or else
) cannot be used,Functions are a collection of statements (organized and reusable code) that perform a specific task, and R has many built-in functions.↩︎