Posts

Showing posts from February, 2023

R Object : S3 vs S4

Image
  ✔ S3 class: Most common class used. Can be applied to multiple data types IE Plot () print () summary (). Can only dispatch on the first argument. Allows you to easily change the class of existing objects (Bad). S3 class has no formal, predefined definition. Basically, a list with its class attribute set to some class name, is an S3 object. The components of the list become the member variables of the object. Assigning a character string to an object's class attribute defines a class in S3, whereas ‘setClass’ function is required in S4. A class can only inherit from another class in S3. Alternatively, S4 supports multiple inheritances, allowing a single category to inherit from multiple others. A class attribute is used to dispatch methods in S3 based on its class attribute. Generic functions are called by R by looking for methods with matching names and class attributes. A subclass of the object's class will be sought if no method is found with a class attribute. Based ...

Mathematics and simulations in Matrices- R Programming

Image
This is an explanation of performing mathematics and simulations in relation to Matrices in R. Two matrices ‘A and B’ are created to work with using the following code and ‘A;B’ is implemented to print the matrices. > A=matrix(c(2,0,1,3), ncol=2) > B=matrix(c(5,2,4,-1), ncol=2) > A;B      [,1] [,2] [1,]    2    1 [2,]    0    3      [,1] [,2] [1,]    5    4 [2,]    2   -1 These two matrices (A & B) can be added using the following code and the resultant is given as ‘C’ . The same  code can be used to add an  ‘n’ number of matrices. > C<- A+B > C      [,1] [,2] [1,]    7    5 [2,]    2    2 Two matrices ‘A & B’ are subtracted using the following code and the resultant is printed as ‘D’ . The same ...

Working with Matrices and Vectors in R

Image
This is an explanation to learn and work with matrices and vectors in RStudio. The matrices ‘A’ from 1 to 100 with 10 rows and ‘B’  from 1 to 1000 with 10 rows is created using the following syntax ‘ y <- matrix(c(1,2,3,4),nrow=2,ncol=2) ’ > A = matrix(1:100, nrow=10) > B = matrix(1:1000, nrow=10) In order to get the transpose of a matrix, transpose function ‘ t() ’ is implemented. Transpose function will turn rows into columns and columns into rows which will be useful when Term Document Matrix is needed from the Document Term Matrix. In addition, it can also be useful in more advanced matrix algebra. #Transpose A and B > t(A) > t(B) Two vectors ‘a and b’ are created using the colon operator to generate integers sequence. #Create two vectors (a and b) > a = c(1:5) > b = c(1:10) The created two matrices ‘A and B’ are multiplied with vectors ‘a and b’ respectively. #Multiply matrices by vectors > X = a*A > Y = b*B Vectors can be re-assigned to equal the n...

Creating Histogram and Boxplot in R

Image
This is an explanation about creating a histogram and boxplot in R through the given data and understanding the outcome of the results from the plots. The names of 5 variables in the dataset are as follows:   ‘ Frequency’,‘BP’, ‘First’ , ‘ Second’, ‘FinalDecision’  1.     "0.6","103","bad","low","low” 2.     "0.3","87","bad","low","high” 3.     "0.4","32","bad","high","low” 4.     "0.4","42","bad","high","high" 5.     "0.2","59","good","low","low” 6.     "0.6","109","good","low","high” 7.     "0.3","78","good","high","low” 8.     "0.4","205","good","high","high” 9.     "0.9","135",”NA","high...