Total Pageviews

Tuesday 4 July 2023

Basics of R programming

 1. Vector: A Vector is a sequence of data elements of the same basic type.

 Example 1: 

>vtr = c(1, 3, 5 ,7 9) 

or 

>vtr<- c (1, 3, 5 ,7 9)

 >print(vtr) 

o/p: [1] 1 3 5 7 9

 Example 2:creating sequence vector by usingcolon operator


>v = 2:12 > print(v) 

o/p: [1] 2 3 4 5 6 7 8 9 10 11 12 

> v = 3.5:10.5 

> v 

o/p: [1] 3.5 4.5 5.5 6.5 7.5 8.5 9.5 10.5 


Accessing vector elements by its position (index).

 > day = c("Mon","Tue","Wed","Thurs","Fri","Sat","sun") 

> print(day[3]) 

o/p: [1] "Wed" 

> weekend=day[c(6,7)] 

> weekend o/p: [1] "Sat" "sun". 

> print(day[c(-2,-3)]) 

// Negative indexing 

o/p: [1] "Mon" "Thurs" "Fri" "Sat" "sun"



2. List: Lists are the R objects which contain elements of different types like − numbers, strings, vectors and another list inside it. A list can also contain a matrix or a function as its elements. List is created using list() function. 

Example 1: 

>n = c(2, 3, 5) 

>s = c("aa", "bb", "cc", "dd", "ee") 

>x = list(n, s, TRUE) >x 

O/p – [[1]] [1] 2 3 5 [[2]] [1] "aa" "bb" "cc" "dd" "ee" [[3]] [1] TRUE


Accessing the elements of list 

>mylist = list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2), list("green",12.3)) 

> print(mylist[1]) [[1]] 

o/p: [1] "Jan" "Feb" "Mar" 

> print(mylist[c(1,3)]) 

o/p: [[1]] [1] "Jan" "Feb" "Mar" [[2]] [[2]][[1]] [1] "green" [[2]][[2]] [1] 12.3 


3. Matrices are the R objects in which the elements are arranged in a two-dimensional rectangular layout. A Matrix is created using the matrix() function. 

Example: matrix (data, nrow, ncol, byrow, dimnames) 

where, 

✓ data is the input vector which becomes the data elements of the matrix. 

✓ nrow is the number of rows to be created. 

✓ ncol is the number of columns to be created. 

✓ byrow is a logical clue. If TRUE then the input vector elements are arranged by row. 


Example:

# Create a matrix. 

>M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE) 

>print(M) 

o/p: [,1] [,2] [,3] [1,] "a" "a" "b" [2,] "c" "b" "a" 

> M = matrix( c('a','a','b','c','b','a'),nrow=3,byrow=FALSE) 

> M [,1] [,2] [1,] "a" "c" [2,] "a" "b" [3,] "b" "a" 


4. Arrays: Arrays are the R data objects which can store data in more than two dimensions. 

 If we create an array of dimension (2, 3, 4) then it creates 4 rectangular matrices each with 2 rows and 3 columns.While matrices areconfined to two dimensions, arrays can be of any number of dimensions. An array is created using the array() function. It takes vectors as input and uses the values in the dim parameter to create an array. 

In the below example we create 2 arrays of which are 3x3 matrices each.


Examples 1: Here we create two arrays with two elements which are 3x3  

\>v1 <- c(5,9,3)

 >v2 <- c(10,11,12,13,14,15) 

>result<- array(c(v1,v2),dim = c(3,3,2)) 

>result 

Output – , , 1

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

[1,] 5 10 13 

[2,] 9 11 14 

[3,] 3 12 15 

, , 2 

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

[1,] 5 10 13 

[2,] 9 11 14

 [3,] 3 12 15 

Data Frames: A data frame is a table or a two-dimensional array-like structure in which each column contains values of one variable and each row contains one set of values from each column. Data frames are tabular data objects. Unlike a matrix in data frame each column can contain different modes of data. The first column can be numeric while the second column can be character and third column can be logical. It is a list of vectors of equal length.Data Frames are created using the data.frame() function. 

open R script in R studio

# Create the data frame. 

BMI <- data.frame( gender = c("Male", "Male","Female"), height = c(152, 171.5, 165), weight = c(81,93, 78), Age = c(42,38,26) ) 

print(BMI) 

#When we execute the above code, it produces the following result − 

    gender     height     weight     Age 

1 Male         152.0     81             42 

2 Male         171.5     93             38 

3 Female     165.0     78             26

No comments:

Post a Comment