Page 221 - Data Science class 11
P. 221
6.6.3 Variable assignment
There are three methods of assignment. The variables can be assigned values using the leftward, rightward, and equal
to operators. In R, the c() function returns a vector (a one-dimensional array).
The values of the variables can be printed using the print() or cat() function. The cat() function combines multiple
items into a continuous print output.
Examples
• Assignment using the equal operator
var.1 = c(0,1,2,3)
• Assignment using the leftward operator
var.2 <- c("learn","R")
In R, the c() function returns a vector (a one-dimensional array).
• Assignment using the rightward operator
c(TRUE,1) -> var.3
print(var.1)
cat ("var.1 is ", var.1,"\n")
cat ("var.2 is ", var.2,"\n")
cat ("var.3 is ", var.3,"\n")
(\n) is a function for newline escape sequence (\n) which creates a new line for the forthcoming text.
6.7 muLtiPLe eLements Vector
When a person writes just one value in R, it becomes a vector of length one and belongs to one of the above-stated
vector types. Such a vector is called a single-element vector.
Just like a single element vector, we also have multiple element vectors. We can create a multiple-element vector with
numeric data using the colon operator.
You can use a vector to create a sequence (say) from 0 to 15. You have to write:
V<- 0:15
print(V)
In the above code snippet, V is a variable that will hold the sequence values from 0 to 15 with an increment of 1.
The print statement is a reserved code word and actually an R function. All code words are case sensitive. The colon
symbol is being used to create the desired sequence. When executed, you will get the following result:
You can generate another sequence with fractional values like the one below:
Example
Enter the following code snippet:
V<- 3.1:10.1
print(V)
Programming with R 219

