To assign a value we use the ‘<-’ symbol.
> hello <- "hello world" ## assign value > print(hello) ## explicit printing  "hello world" > hello <- 100 > hello ## auto-print  100
Like in usual programming language after assigning the value to test/evaluate, we can use print keyword to print the value. The above example was executed in console window. So it’s much easier to just type the variable to show the values rather than explicitly print. When we are printing R vector we can see [ ] brackets. In our above example its shown in the output indicates that ‘hello’ is a vector and 100 is its first element.
Unlike other programming language R doesn’t support multiple line comments. Therefore, only single line comment with ‘#’ character work with R language.
In any programming language we need various variables to implement our logics. Comparing to other programming language R, variables are not declared as some data type. The mostly used R-objects are,
- Data Frames
The basic type of R object is a vector. A vector can only contain objects of the same class. It can contain the following basic classes of objects:
> v <- c(T,F) ## logical > print(class(v))  "logical" > v <- c(0.2,1) ## number > print(class(v))  "numeric" > v <- c('a','c') ## character > print(class(v))  "character" > v <- c(1+0i, 2+4i) ## complex > print(class(v))  "complex" > v <- 1:10 ## integer > print(class(v))  "integer"
If you want to assign more than one element in a vector, we should use ‘c()’ function.
> hello <- c('h','e','l','l','0') > hello  "h" "e" "l" "l" "0"
A list represents has vector but can contain different objects of classes.
> helloList <- c(hello,12,23,34.44) > helloList  "h" "e" "l" "l" "0" "12" "23" "34.44"
It’s like vectors but with ‘dimension’ attributes (two-dimensional data set). It contains row and column size.
> helloMat <- matrix(nrow = 2, ncol = 3) > helloMat [,1] [,2] [,3] [1,] NA NA NA [2,] NA NA NA
Unlike matrices array can be created with any number of dimensions.
> helloArr <- array(dim = c(4,4)) > helloArr [,1] [,2] [,3] [,4] [1,] NA NA NA NA [2,] NA NA NA NA [3,] NA NA NA NA [4,] NA NA NA NA
It’s used to store categorical data. Factors are important in statistical modeling and this feature will be very handy to use. Basically its like keeping label in a vector.
> helloList <- c('hello', 'world','world','hello','world') > helloFac <- factor(helloList) > helloFac  hello world world hello world Levels: hello world > table(helloFac) helloFac hello world 2 3
It’s something similar to matrices but with different data objects in columns. Therefore, it helps to store tabular data in R Language.
> helloDF <- data.frame(num = 1:3, name = c('Renien', 'John', 'Joseph')) > helloDF num name 1 1 Renien 2 2 John 3 3 Joseph