R in Clinical World: WEEK2
1. Working with R
An object can be created on the fly using “<-” or “->” or “=” and also we can remove objects from environment using ‘rm()’.
PKAA=”PinnacleVex KA Analytics”
PKAAx <- rnorm(100,mean=20,sd=5)
x
mean(x)
m <- mean(x)
m
std<-sd(x)
sd
std#Removing an object
rm(PKAA,x,std)
PKAA
x
std
2. R packages:
Packages are collections of functions, complied code, and sample data sets, and are stored under a directory called “library” in the R environment. By default, a set of packages(approximately 30) are installed during installation of base R. We can add required packages when they are needed for some specific purpose. For example, if we are working with data frames then probably we will use dplyr, or data.
There are now more than 16,000 R packages available for download.
Install Package using GUI:
Select the ‘Packages’ menu, and then select ‘Install R Packages’, a list of available packages on your system will be displayed, then select one and click ‘OK’, the package is attached to your current R session via the library function.
If we want to use any function, we need to install the package that contains it.
Install Package from CRAN:
install.packages(“<the package’s name>”)
library(“<the package’s name>”)
Install Package from GitHub:
We can use the install_github function from the remotes package.
remotes::install_github(“githubaccountname/packagename”)
Note: Download & Install Package, then Load a package and finally don’t forget to load the package after installation. Installed R package can be used only after attaching it.
Practice: Installing packages
Q. Draw a 3d scatter plot of three random vectors x, y, z of size 10000.
Note: Use rnorm() function to create these vectors and scatterplot3d package.
Answer:
#Installing package
install.packages(“scatterplot3d”)
#Load a package
library(scatterplot3d)# Creating random vectors x, y, z
x <- rnorm(1000,mean=15,sd=3)
y <- rnorm(1000,mean=20,sd=5)
z <- rnorm(1000,mean=25,sd=8)
x
y
z#plot
scatterplot3d(x,y,z)
3. List of some useful R packages:
There many useful R packages written by R’s active user community. Here i’m listing some of them.
A. Data handling Packages:
To Load Data:
RODBC, DBI, RMySQL, RPostgresSQL, RSQLite, googleAuthR, cloudyR project, downloader, XLConnect, xlsx, foreign, and haven.
To Manipulate Data:
dplyr, data.table , parallel, purrr, tidyr, plyr, reshape2, stringr, zoo, lubridate
B. Data visualization packages:
ggplot2, plotly, shiny, ggvis, rgl, dygraphs, htmlwidgets and rcdimple.
C. Data Analysis/ Modelling Packages:
survival, car, caret, mgcv, randomForest, Forecast, nnet, lme4/nlme, multcomp, vcd, glmnet, e1071.
D. Reporting Packages:
R Markdown, knitr, shiny, xtable, atable, ClinReport, R3port, greport, hreport, gt, dt, formattable, flextable, reactable, huxtable, officer and reporter.
Happy Learning !!! :)
Related Articles:
PinnacleVex KA Analytics: BASICS OF MICROSOFT EXCEL
PinnacleVex KA Analytics: Basic Excel Formulas For Your Daily Workflow
PinnacleVex KA Analytics: Simple introduction to SQL