Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

R Cheat Sheet with Basic Commands, Functions and Plot, Cheat Sheet of Advanced Computer Programming

R reference card document teaching basic coding with examples

Typology: Cheat Sheet

2020/2021

Uploaded on 04/26/2021

sureesh
sureesh 🇺🇸

4.8

(9)

243 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CCR
Summer 2013
R Reference Sheet
Eric Pitman Annual Summer Workshop in Computational Science
Author: C. Ryan Mraz
NAME: _____________________________
pf3
pf4
pf5

Partial preview of the text

Download R Cheat Sheet with Basic Commands, Functions and Plot and more Cheat Sheet Advanced Computer Programming in PDF only on Docsity!

CCR

Summer 2013

R – Reference Sheet

Eric Pitman Annual Summer Workshop in Computational Science

Author: C. Ryan Mraz

NAME: _____________________________

--------------------------------------------------------------------- RStudio Tips -------------------------------------------------------------------

 There is no editor window until you open up a file! To do so, click:

 To see your history (commands you have already issued), click the history pane or

simply click the up arrow on your keyboard while on the command line

 To change the relative sizes of each window, hover the mouse over the window border

until appears.

 Is your project loaded? Check the upper right Corner:

 There are two ways to load csv files in Rstudio:

  1. In the RStudio Workspace:

Select Import Dataset: From Text File

Select a .csv file to Open

Use Heading=Yes

  1. From the command line:

Set the Working Directory

Load command:

drop=read.csv(“drop.csv”)

*Keep Your Projects Tidy!!

 To clear the Console window, use: ctrl + L

 To clear individual items in the Workspace, use: r m(variable_name)

 To clear all items in the Workspace or plotspace, use:

Here: Then Here:

-----------------------------------------------------------Conditionals/Function Calls---------------------------------------------------------

if (condition is true) {

do something

}

functionName =function(inputs) {

---------------- # do something

return (------) #result

}

---------------------------------------------------------------- Common Plots --------------------------------------------------------------------

example scatterplot:

data(diamonds, package=”ggplot2”)

plot( formula=price~carat,

data=diamonds,

col=”darkblue”,

pch=20,

main="Diamond Price with Size"

)

example barplot:

data(diamonds, package=”ggplot2”)

ideal=diamonds[diamonds$cut=="Ideal","color"]

barplot( table(ideal),

xlab="color",

ylab="count",

main="Ideal cut diamonds by Color",

col="hotpink" )

example histogram:

data(Cars, package=”MASS”)

hist( Cars93$RPM,

breaks = 4,

xlab="RPM",

main="histogram of engine RPM",

col="red"

)

example density plot:

data(Cars, package=”MASS”)

plot( density(Cars93$RPM,bw=200),

main="Density Curve of Engine RPMs of 93 Cars",

xlab="RPM",

col="blue"

)

example boxplot:

boxplot(formula=mpg~gear,

data=mtcars,

main="Mileage by Gear Number",

xlab="Number of Gears",

ylab="Miles Per Gallon",

col=c("red","green","blue")

)

#-------------------------------------------------------------- Apply Family ---------------------------------------------------------

# There are many types of the function apply, but for our purposes, we will only be using sapply.

sapply

The apply() family of functions can be used to call some other function multiple times on a dataset, with several different arguments. sapply() returns a vector or matrix result. You can use sapply() on a native R function, or on a function you wrote yourself.

EXAMPLE:

u=c(33,45,37,50) # Creating Vector u v=c(2,5,8,11) # Creating Vector v d=data.frame(u=u,v=v) # Creating Data frame d from Vectors u and v

d # This is what our data frame looks like: u v # 4 rows of 2 columns 1 33 2 2 45 5 3 37 8 4 50 11

sapply( d, mean) # Here, we apply the mean function to our data frame

using sapply

u v # sapply applies the mean function to each column of 41.25 6.50 # the data frame and outputs each answer in a user-

friendly format