Posts

Showing posts from March, 2023

'DEBUGGING' in R

Image
The code below contains a 'deliberate' bug that must be rectified. tukey_multiple <- function(x) {   outliers <- array(TRUE,dim=dim(x))   for (j in 1:ncol(x))   {     outliers[,j] <- outliers[,j] && tukey.outlier(x[,j])   }   outlier.vec <- vector(length=nrow(x))   for (i in 1:nrow(x))   { outlier.vec[i] <- all(outliers[i,]) } return(outlier.vec) } When the above code is implemented, it will result in the following error: Error: unexpected symbol in: " for (i in 1:nrow(x)) { outlier.vec[i] <- all(outliers[i,]) } return" The function 'traceback' prints the list of functions that were called before the error occurred. The function 'source', which reads and executes the code, can be used to find the exact location of the error, which will help to rectify the error. So, when the 'source' function is implemented, it gives the location of the error just before ' return' . > traceback("tukey_multipl...

Building 'PACKAGE' and 'DESCRIPTION' file in R

Image
Package in R is a structured, standardized unit consisting of 1. R code 2. Documentation 3. Data 4. External code Creating a package is initiated by generating a new blank project in R. Installing ' devtools ' and ' roxygen ' which help in creating packages and can be done by the following code: install.packages(“devtools”) install.packages(“roxygen2”) library(devtools) library(roxygen2) package.skeleton() It can also be installed directly by implementing code   devtools::install_github("hadley/devtools") It creates new files: Data, folder  DESCRIPTION, information file  Man, help folder R folder contains the . R files for functions  And “read-and-delete-me” file The Description consists of  fundamental information regarding the package in the following sequence: The important and required fields are ‘Package’, ‘Version’, ‘License’, ‘Description’, ‘Title’, ‘Author’, and ‘Maintainer’ and other fields are optional. Package: Veda Title: Fun...

Visualization of Graphics in R

Image
To understand graphics in R, initially dataset is selected to understand it.  The dataset 'measles' is downloaded from the following link:  https://vincentarelbundock.github.io/Rdatasets/datasets.html The dataset 'measles' is imported  in R by clicking on import dataset option below the environment tab in Rstudio followed by appearanace of dialog box to choose data set from downloads. To produce graphics in R, there are three types: 1. Base graphics 2. Lattice 3. ggplot2 1. Base graphics: Base graphics already exists in the R installation. It allows to create a wide range of two dimensional plots, including scatterplots, line graphs, bar graphs, and more. > plot(measles,pch=21,col=rainbow(12),main= " Deaths in London from measles", cex=1.0) > plot(measles,pch=21,bg="green",col=cm.colors(12),main= " Deaths in London from measles", cex=1.0) 'plot' function  is implemented  which creates scatterplot of a dataset. 'pch=21'...

Input/Output, String manipulation and 'plyr' package in R

Image
✔ This is initiated by creating a dataset in a text file. ‘read.table’ function can be used to read a table from a file in R . ‘file. choose’ opens a dialogue box which allows choosing the text file which was created earlier. ‘header=TRUE’ indicates that the first row consists of column names. ‘sep=","’ code separates columns in the table by commas. > #Imports dataset into R and assigns as ‘x’ > x=read.table(file.choose(),header=TRUE,sep=",") > x         Name Age    Sex Grade 1       Raul  25   Male    80 2     Booker  18   Male    83 3      Lauri  21 Female    90 4     Leonie  21 Female    91 5    Sherlyn  22 Female    85 6    Mikaela  20 Female    69 7 ...