Tuesday, 19 April 2016

Matrices

Matrices:

Here matrices are two dimensions arrays. 

> matrixone = matrix(c(1,2,3,4,5,6,7,8), nrow=2, ncol = 4, byrow=TRUE)

here we assigning a matrix 
> print(matrixone)
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8

Lists

Lists

A container of different elements or different data types.

> listone <- list(c(1,2,3),"your name",TRUE,2L,1+2i,2.36)
Assigning different type of values in a container called listone which is of type list

Printing the listone
> print(listone)

Output
[[1]]
[1] 1 2 3

[[2]]
[1] "your name"

[[3]]
[1] TRUE

[[4]]
[1] 2

[[5]]
[1] 1+2i

[[6]]
[1] 2.36


Vectors

Vectors:

--------------------------------------------------------------------------------------------------------------------------
there are six types in the Vectors:
--------------------------------------------------------------------------------------------------------------------------

Logical -- when  a value is true or false, then it shoes the class of the value:
for example:

> vectorone <- TRUE

assigning TRUE to the vectorone variable 

> print(vectorone)
printing thevariable

Output
[1] TRUE


> print(class(vectorone))
printing the class of the vectorone
 
Output:
[1] "logical"

--------------------------------------------------------------------------------------------------------------------------
> vectorone <- 26.5

assigning the numerical value for variable vectorone

> print(vectorone)
printing the vectorone

Output
[1] 26.5

> print(class(vectorone)) 
printing the class of the vector

Output:
[1] "numeric"


> vectorone <- 23

assigning the value for the vectorone

> print(vectorone)
printing the value

Output:
[1] 23

> print(class(vectorone))
printing the class of the variable
 Output
[1] "numeric"
------------------------------------------------------------------------------------------------------------------------- 
> vectortwo<-"your name"

Assigning the value for vectortwo

> print(vectortwo)
printing the value of the variable

Output
[1] "your name"

printing the class of the variable

Output
> print(class(vectortwo))
[1] "character"
--------------------------------------------------------------------------------------------------------------------------
 vector3 <- 1L
Assigning the value for the vector3

printing the vector3
> print(vector3)

Output:
[1] 1

printing the class of the vector3 
> print(class(vector3))

Output:
[1] "integer"

--------------------------------------------------------------------------------------------------------------------------
> vector4 <- 1+2i
assigning the complex value for the vector4

> print(vector4)
printing the vector4

Output
[1] 1+2i

printing the class of the vector3

> print(class(vector4))

Output
[1] "complex"
--------------------------------------------------------------------------------------------------------------------------
To create vector more than one value , we use c() it is a combiner function then the syntax ill be in the following format, 

> vector5 <- c("firstname",2L,23.6,2+3i,TRUE)
assigning multiple values to the vector5

> print(vector5)
printing vector5

Output
[1] "firstname" "2"         "23.6"      "2+3i"      "TRUE" 

Printing the class of the vector5:

Output    
> print(class(vector5))
[1] "character"




Data Types:



We don't declare the data types in front of the variables. the Data type of the R language will become the Data type of the Variable 

Some of the Data type Objects in the R language are:
1. Vectors.
2.Matrices.
3. Data Frames.
4. Lists.
5. Arrays.
6. Factors.


Lets have go further for the vectors.

Strings:

In script write commands:

#writing the strings in R language
myname <- "your name"
print (myname)

Output:
[1] "your name"

Explanation:
In the first line # is defining the comment. R supports only the single line comment.

Here myname is a variable which stores the the string literal "your name"
and when a print command issued on the myname then it prints the string literal.

Here you can use single quotes or double quotes . the output will be same:

myname <- 'your name'
print(myname)

Output:
[1] "your name"

Explanation:
In the output the string literal will be in the double quotes in the two cases.

--------------------------------------------------------------------------------------------------------------------------
> vectortwo<-"your name"

Assigning the value for vectortwo

> print(vectortwo)
printing the value of the variable

Output
[1] "your name"

printing the class of the variable

Output
> print(class(vectortwo))
[1] "character"

First R Script:

Create a new Script and Write the following:
 print('Hai world welcome to R language tutorials')

the Output will be:
[1] "Hai world welcome to R language tutorials"