R Cheat Sheet

Have an interview tomorrow? Facing last moment preparation issues? Well we bring to you this Base R Cheat Sheet to ace up your chances of getting employed.

As Data Exploration and Data Manipulation takes most of the time, chances of being questioned as to how shall you deal with an unstructured data and manipulate them are high.

Getting Started with Libraries

install.packages(“dplyr”)

Allows download and install packages from CRAN repository.

library(dplyr) :

Loads the package in the session in order to allow use of the functions.

Working Directory

getwd()

To get the current location of the directory.

setwd(“file path”)

To change the current working directory.

Reading and Writing Data

data <- read.csv(“file.csv”)

To read or import data as CSV file.

write.csv(data, “file.csv”)

To write or export data.

Converting Data Types

as.logical

To convert into Boolean form

as.numeric

To convert to numeric

as.factor

To convert into factors

Mathematical Functions

log(x)

To calculate natural log.

exp(x)

To calculate exponential

round(x,n)

To round a number to n decimal places

corr(x,y)

To find correlation between 2 variables

Dealing with Data Frames

df <- data.frame(x = 6:10, y = c( ‘ a’, ‘b’, ‘c’)

Creating a data frame

df$x

Accessing a column of a data frame

view(df)

To look at the whole data set

dim(df)

number of rows and columns in a data frame

rbind

binding rows

cbind

binding columns

Dealing with strings

paste(x, y, sep =’ ‘)

To join multiple vectors together

grep(pattern, x)

To find regular expression matches in x

gsub(pattern, replace, x)

To replace matches in x with a string

toupper(x)

To convert a string to upper case

nchar(x)

To find the number of characters in the string

Plotting data

plot(x, y)

Bivariate plot

hist(x)

Plotting a histogram

barplot(x)

Plotting barplots

pie(x)

Plotting a piechart

stripplot(x)

Plotting values of x on a line (an alternative to boxplot for small values)

Statistics

lm(formula, data=)

Fitting a linear regression model

glm(formula, data=, family=)

Fitting a generalized linear model

predict(fit)

To predict on the fitted model

fitted(fit)

To obtain fitted values

Team Ivy

Leave a Reply

Your email address will not be published. Required fields are marked *