Creating and Manipulating Objects
Creating and Manipulating Objects
Every programming language has its own data types to store values or any information so
that the user can assign these data types to the variables and perform operations
respectively. Operations are performed accordingly to the data types.
These data types can be character, integer, float, long, etc. Based on the data type,
memory/storage is allocated to the variable..
Unlike other programming languages, variables are assigned to objects rather than data
types in R programming.
Type of Objects
There are 5 basic types of objects in the R language:
Vectors
Atomic vectors are one of the basic types of objects in R programming. Atomic vectors can
store homogeneous data types such as character, doubles, integers, raw, logical, and
complex. A single element variable is also said to be vector.
Example:
# Create vectors
x <- c(1, 2, 3, 4)
z <- 5
print(x)
print(class(x))
print(y)
print(class(y))
print(z)
print(class(z))
Output:
[1] 1 2 3 4
[1] "numeric"
[1] "a" "b" "c" "d"
[1] "character"
[1] 5
[1] "numeric"
Lists
List is another type of object in R programming. List can contain heterogeneous data types
such as vectors or another lists.
Example:
# Create list
print(ls)
print(class(ls))
Output:
[[1]]
[1] 1 2 3 4
[[2]]
[[2]][[1]]
[1] "a"
[[2]][[2]]
[1] "b"
[[2]][[3]]
[1] "c"
[1] "list"
Matrices
To store values as 2-Dimensional array, matrices are used in R. Data, number of rows and
columns are defined in the matrix() function.
Syntax:
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
Example:
x <- c(1, 2, 3, 4, 5, 6)
# Create matrix
print(mat)
print(class(mat))
Output:
[, 1] [, 2] [, 3]
[1, ] 1 3 5
[2, ] 2 4 6
[1] "matrix"
Factors
Factor object encodes a vector of unique elements (levels) from the given data vector.
Example:
# Create vector
"spring", "autumn")
print(factor(s))
print(nlevels(factor(s)))
Output:
[1] spring autumn winter summer spring autumn
Levels: autumn spring summer winter
[1] 4
Arrays
array() function is used to create n-dimensional array. This function takes dim attribute as
an argument and creates required length of each dimension as specified in the attribute.
Syntax:
array(data, dim = length(data), dimnames = NULL)
Example:
print(arr)
Output:
,, 1
[, 1] [, 2] [, 3]
[1, ] 1 1 1
[2, ] 2 2 2
[3, ] 3 3 3,, 2
[, 1] [, 2] [, 3]
[1, ] 1 1 1
[2, ] 2 2 2
[3, ] 3 3 3,, 3
[, 1] [, 2] [, 3]
[1, ] 1 1 1
[2, ] 2 2 2
[3, ] 3 3 3
Data Frames
Data frames are 2-dimensional tabular data object in R programming. Data frames consists
of multiple columns and each column represents a vector. Columns in data frame can have
different modes of data unlike matrices.
Example:
# Create vectors
x <- 1:5
y <- LETTERS[1:5]
df <- data.frame(x, y, z)
print(df)
Output:
xy z
1 1 A Albert
22B Bob
3 3 C Charlie
4 4 D Denver
55E Elie
Manipulating Objects
Objects can be manipulated by using several functions.
Modify Objects
edit() function will invokes a text editor on an R object
edit() works with any type of object
o Vector
o List
o Matrix
o Data Frame
o Factor
Consider an object newData
edit(newData) invokes an editor in which the data can be modified. The example shown
illustrates the same
Syntax
modData <- edit(newData)
Data Editor window will pop up and data can be edited in the same window. For vectors,
lists and factors, a notepad will pop up.
Where,
x: the vector
value: the value to be appended
[1] 1 2 3 4 5 6 7 8 9
[10] 10 12 13 14 15 16 17 18 19
[19] 20
Delete Objects
remove() and rm() can be used to remove objects. These can be specified successively as
character strings or in the character vector list or through a combination of both. All objects
thus specified will be removed
Consider the following objects in R
ls()
[1] "a" "b" "c" "d" "newData" "trees"
If we want to delete ‘b’ and ‘trees’ objects
rm("b", "trees")
ls()
[1] "a" "c" "d" "newData"
If we want to remove all the objects in R, use rm(list =ls())
rm(list=ls())
Converting Objects
type.convert() function in R Language is used to compute the data type of a particular data
object.
It can convert data object to logical, integer, numeric, or factor.
Syntax: type.convert(x)
Parameter:
x: It can be a vector matrix or an array
Example 1: Apply type.convert to vector of numbers
# R program to illustrate
class(x1_convert)
Output:
[1] "integer"
Here in the above code, we can see that before type.convert() function all the numbers are
stored in it, but still its a character vector. And after the function it became “Integer”.
Example 2: Type conversion of a vector with both characters and integers.
# R Program to illustrate
# conversion of a vector with both characters and
integers
class(x2)
class(x2_convert)
Output:
[1] "character"
[1] "factor"
Here, in the above code there were some characters and some integers after using
type.convert() function. So, the output comes to be a factor.
There are multiple ways to access or replace values in vectors or other data structures. The
most common approach is to use “indexing”. This is also referred to as “slicing”.
Brackets [ ] are used for indexing.
Here are some examples that show how elements of vectors can be obtained by indexing.
b <- 10:15
b
## [1] 10 11 12 13 14 15
Get the first element of a vector
b[1]
## [1] 10
Get elements 2 and 3
b[2:3]
Now a more advanced example, return all elements except the second
b[c(1,3:6)]
You can also use an index to change values
b[1] <- 11
# Creating a vector
x <- c(1, 2, 3, 4, 5)
names(x)
print(x)
Output:
[1] "gfg1" "gfg2" "gfg3" "gfg4" "gfg5"
gfg1 gfg2 gfg3 gfg4 gfg5
1 2 3 4 5
Accessing Specific Variables in an Object
print(m1$sex)