Está en la página 1de 10

R version 3.3.

3 (2017-03-06) -- "Another Canoe"

Copyright (C) 2017 The R Foundation for Statistical Computing

Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.

You are welcome to redistribute it under certain conditions.

Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.

Type 'contributors()' for more information and

'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or

'help.start()' for an HTML browser interface to help.

Type 'q()' to quit R.

[Previously saved workspace restored]

#R can be used for algebraic calculation

> 1+1

[1] 2

> 1-1

[1] 0

> variable1=1+1
> variable1

[1] 2

> variable1 <- 1 + 1

> variable1

[1] 2

> 1 + 2 / 3 - 2 * 6.5

[1] -11.33333

> 1 * (2 / (1 + 1))

[1] 1

>1*2/1+1

[1] 3

#Vector is a set of number either is a single row or column, with one direction

> vector1 = 1:9

> vecter1

Error: object 'vecter1' not found

> vector1

[1] 1 2 3 4 5 6 7 8 9

> 1.2:9

[1] 1.2 2.2 3.2 4.2 5.2 6.2 7.2 8.2

> vector2 = c(1, 3, 2, -8.1)

> vector2

[1] 1.0 3.0 2.0 -8.1

> vector3 = 1:9

> vector3

[1] 1 2 3 4 5 6 7 8 9

> vector4=9:1
> vector4

[1] 9 8 7 6 5 4 3 2 1

> vector3+vector4

[1] 10 10 10 10 10 10 10 10 10

> vector5=1:3

> vector3

[1] 1 2 3 4 5 6 7 8 9

> vector3+vector5

[1] 2 4 6 5 7 9 8 10 12

#R can be used to represent matrix, matrix calculation and even picking particular
element from a matrix.

> matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3)

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

> matrix(1:8, ncol = 2)

[,1] [,2]

[1,] 1 5

[2,] 2 6

[3,] 3 7

[4,] 4 8

> matrix(vector3, nrow = 3)

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9
> matrix(vector3, nrow = 2)

[,1] [,2] [,3] [,4] [,5]

[1,] 1 3 5 7 9

[2,] 2 4 6 8 1

Warning message:

In matrix(vector3, nrow = 2) :

data length [9] is not a sub-multiple or multiple of the number of rows [2]

> 1+1

[1] 2

> vector3

[1] 1 2 3 4 5 6 7 8 9

> matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), ncol = 9)

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]

[1,] 1 2 3 4 5 6 7 8 9

> matrix1 = matrix(vector3, nrow = 3)

> matrix1 + 2

[,1] [,2] [,3]

[1,] 3 6 9

[2,] 4 7 10

[3,] 5 8 11

> matrix1 + matrix1

[,1] [,2] [,3]

[1,] 2 8 14

[2,] 4 10 16

[3,] 6 12 18

> matrix1%*%matrix1
[,1] [,2] [,3]

[1,] 30 66 102

[2,] 36 81 126

[3,] 42 96 150

> matrix1 * matrix1

[,1] [,2] [,3]

[1,] 1 16 49

[2,] 4 25 64

[3,] 9 36 81

#matrix manipulation can be done using various functions as below.

> t(matrix1)

[,1] [,2] [,3]

[1,] 1 2 3

[2,] 4 5 6

[3,] 7 8 9

> matrix1

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

> matrix1[1, 3]

[1] 7

> matrix1[ 2, ]

[1] 2 5 8

> matrix1[,-2]

[,1] [,2]
[1,] 1 7

[2,] 2 8

[3,] 3 9

> matrix1[1, 1] = 15

> matrix1

[,1] [,2] [,3]

[1,] 15 4 7

[2,] 2 5 8

[3,] 3 6 9

> matrix1[ ,2 ] = 1

> matrix1

[,1] [,2] [,3]

[1,] 15 1 7

[2,] 2 1 8

[3,] 3 1 9

> matrix1[ ,2:3 ] = 2

> matrix1

[,1] [,2] [,3]

[1,] 15 2 2

[2,] 2 2 2

[3,] 3 2 2

> matrix1[ ,2:3 ] = 4:9

> matrix1

[,1] [,2] [,3]

[1,] 15 4 7

[2,] 2 5 8
[3,] 3 6 9

#replacing first columns matrix element with value from vector5

> matrix1[ ,1 ] = vector5

> matrix1

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

#prints matrix element greater than 5

> matrix1[matrix1 > 5]

[1] 6 7 8 9

#Gives Boolean result

> matrix1 > 5

[,1] [,2] [,3]

[1,] FALSE FALSE TRUE

[2,] FALSE FALSE TRUE

[3,] FALSE TRUE TRUE

> matrix1[matrix1 >= 8] = 3

> matrix1

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 3

[3,] 3 6 3

#for loop in R

> for (i in 1:3) { + print(i)+ print(i + 5) }

[1] 1
[1] 6

[1] 2

[1] 7

[1] 3

[1] 8

> matrix3=matrix1+5

> print(matrix3)

[,1] [,2] [,3]

[1,] 6 9 12

[2,] 7 10 8

[3,] 8 11 8

#for loop to replace matrix3 element with element from matrix1

> for (i in 1:3) { matrix3[i, ] = matrix1[i, ] }

> print(matrix3)

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 3

[3,] 3 6 3

> print(matrix1)

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 3

[3,] 3 6 3

#for loop to replace matrix element with 0 first row and first column, second row
and first column and third row and first column with zero where if element value is
greater than or equal to 2.

> for( i in 1:3 ){ if(matrix1[i,1] >= 2) { matrix1[i,1] = 0 } }


> print(matrix1)

[,1] [,2] [,3]

[1,] 1 4 7

[2,] 0 5 3

[3,] 0 6 3

> vector6 = 9:7

> vector6

[1] 9 8 7

#While loop which checks if sum of vector6 is greater than or equal to 6 then it will
execute and print value of vector6 by subtracting 1 from each value till sum of
vector6 < 6.

> while(sum(vector6) >= 6) {

+ vector6=vector6-1

+ print(vector6)

+}

[1] 8 7 6

[1] 7 6 5

[1] 6 5 4

[1] 5 4 3

[1] 4 3 2

[1] 3 2 1

[1] 2 1 0

#if element in matrix1 is greater than 5 then print 1 else print 0.

> ifelse(matrix1 > 5, 1, 0)

[,1] [,2] [,3]

[1,] 0 0 1

[2,] 0 0 0
[3,] 0 1 0

>

También podría gustarte