![]() |
VOOZH | about |
One of the most interesting facts about R is, what is known as objects in R are known as variables in many other programming languages. Depending on the context objects and variables can have drastically different meanings. In every computer language variables provide a means of accessing the data stored in memory. R does not provide direct access to the computer’s memory but rather provides several specialized data structures we will refer to as objects. These objects are referred to through symbols or variables. In R, however, the symbols are themselves objects and can be manipulated in the same way as any other object. This is different from many other languages and has wide-ranging effects. All entities in R can be called as objects. They can be arrays, numbers, strings, lists, vectors, data frames, etc.
To do useful and interesting stuff in R, one needs to assign values to objects. To create an object in R, one needs to give it a name followed by the assignment operator <- (An equal sign, =, can also be used), and the value he wants to give it:
x <- 5
<- is the assignment operator. It assigns values on the right to objects on the left. So, after executing x <- 5, the value of x is 5. The arrow can be read as 5 goes into x.
Naming rules for objects:
Example:
Output:
[1] 5 [1] "Amiya" [1] 1 2 3 [1] "A" "B" "C" $Numbers [1] 1 2 3 $Characters [1] "A" "B" "C" Numbers Characters 1 1 A 2 2 B 3 3 C
One can list all the objects in his working directory by using objects() or ls() function. objects() or ls() function can be used to get a vector of character strings of the names of all objects in the environment.
Example:
Output:
[1] 5 [1] "Amiya" [1] 1 2 3 [1] "A" "B" "C" $Numbers [1] 1 2 3 $Characters [1] "A" "B" "C" Numbers Characters 1 1 A 2 2 B 3 3 C Using object() [1] "listOfNumber" "myDataFrame" "name" "vec1" "vec2" [6] "x" Using ls() [1] "listOfNumber" "myDataFrame" "name" "vec1" "vec2" [6] "x"
One may notice that the objects() or ls() function returns the result in a sorted order.
Listing objects that satisfy a particular pattern: In R it is also possible to list objects that specify a particular regular expression.
Example:
Output:
[1] 5 [1] "Amiya" [1] 1 2 3 [1] "A" "B" "C" $Numbers [1] 1 2 3 $Characters [1] "A" "B" "C" Numbers Characters 1 1 A 2 2 B 3 3 C [1] "vec1" "vec2" [1] "myDataFrame" "name"
One can delete the objects in his working directory by using rm() or remove() function. rm() or remove() function can be used to free up the memory and clear the environment space.
Example:
Output:
[1] 5 [1] "Amiya" [1] 1 2 3 [1] "A" "B" "C" $Numbers [1] 1 2 3 $Characters [1] "A" "B" "C" Numbers Characters 1 1 A 2 2 B 3 3 C After deleted following objects listing of the object: [1] "listOfNumber" "name" "vec1" "vec2"
One can also delete all objects inside the memory by just passing an argument list = ls() to the function rm() or remove().
Example:
Output:
[1] 5 [1] "Amiya" [1] 1 2 3 [1] "A" "B" "C" $Numbers [1] 1 2 3 $Characters [1] "A" "B" "C" Numbers Characters 1 1 A 2 2 B 3 3 C After deleted all objects listing of the object: character(0)