Go to TogaWare.com Home Page. Data Science Desktop Survival Guide
by Graham Williams
Duck Duck Go



CLICK HERE TO VISIT THE UPDATED SURVIVAL GUIDE

Variables

20200501 We can store values, such as the resulting value from invoking a function (Section 3.5), into a variable. A variable is a name that we can use to refer to some location in the computer's memory. Into this location we store the data while our programs are running so that we can refer to that data by the specific variable name later on. We use the assignment operator base::$<$- to do so:
# Add two numbers and assign the result into a variable.

v <- sum(1, 2)

We can access the value stored in the variable (or actually stored in the computer's memory that the variable name refers to) simply by requesting through the R Console that R “run” the command v. We do so below with a line containing just v. In fact the R software actually runs the base::print() function to display the contents of the computer's memory that v refers to—this is a convenience that the R software provides for us.

# Print the value stored in a variable.

v
## [1] 3
print(v)
## [1] 3

By indexing the variable with [1] we can ask for the first item referred to by v. Noting that the variable v is a vector with only a single item when we try to index a second item we get an NA (meaning not available or a missing value).

# Access a particular value from a vector of data.

v[1]
## [1] 3
v[2]
## [1] NA


Support further development by purchasing the PDF version of the book.
Other online resources include the GNU/Linux Desktop Survival Guide.
Books available on Amazon include Data Mining with Rattle and Essentials of Data Science.
Popular open source software includes rattle and wajig.
Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 2000-2020 Togaware Pty Ltd. . Creative Commons ShareAlike V4.