



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
R reference card document teaching basic coding with examples
Typology: Cheat Sheet
1 / 7
This page cannot be seen from the preview
Don't miss anything!
--------------------------------------------------------------------- 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:
Select Import Dataset: From Text File
Select a .csv file to Open
Use Heading=Yes
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) {
}
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")
)
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.
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
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-