Docsity

RStudio Cheat Sheet with Commands Examples, Cheat Sheet of Advanced Computer Programming

Toggle sidebar
bg1
Cheat Sheet for R and RStudio
L. Jason Anastasopoulos
April 29, 2013
1 Downloading and Installation
•First download R for your OS: R
•Next download RStudio for your OS: RStudio
2 Uploading Data into R-Studio
R-Studio Makes uploading CSV files into R extremely simple. Just follow these instructions
and you’ll be using R in no time.
1. Download your .csv data to a folder that you can easily find.
2. Open R-Studio.
3. In the interpreter (lower left-hand box of RStudio), type library(foreign) and hit
Enter. This will install the package that reads your .csv files.
4. In the box on the upper-right hand corner of RStudio, click on the tab that says
ā€œWorkspaceā€.
5. Then click on ā€œImport Dataset >From Text File...ā€. Find your .csv dataset and open
it.
6. In the interpreter (lower left-hand box), type in attach(name-of-dataset) and hit En-
ter. You can find the name of the dataset listed under the ā€œWorkspaceā€ tab in the
upper right-hand corner of RStudio.
7. To find the variable names in your dataset type names(name-of-dataset) and hit
Enter.
1
pf3
pf4
pf5

Partial preview of the text

Download RStudio Cheat Sheet with Commands Examples and more Cheat Sheet Advanced Computer Programming in PDF only on Docsity!

Cheat Sheet for R and RStudio

L. Jason Anastasopoulos

April 29, 2013

1 Downloading and Installation

  • First download R for your OS: R
  • Next download RStudio for your OS: RStudio

2 Uploading Data into R-Studio

R-Studio Makes uploading CSV files into R extremely simple. Just follow these instructions and you’ll be using R in no time.

  1. Download your .csv data to a folder that you can easily find.
  2. Open R-Studio.
  3. In the interpreter (lower left-hand box of RStudio), type library(foreign) and hit Enter. This will install the package that reads your .csv files.
  4. In the box on the upper-right hand corner of RStudio, click on the tab that says ā€œWorkspaceā€.
  5. Then click on ā€œImport Dataset > From Text File...ā€. Find your .csv dataset and open it.
  6. In the interpreter (lower left-hand box), type in attach(name-of-dataset) and hit En- ter. You can find the name of the dataset listed under the ā€œWorkspaceā€ tab in the upper right-hand corner of RStudio.
  7. To find the variable names in your dataset type names(name-of-dataset) and hit Enter.

3 Doing Statistics in RStudio

After you have opened your data, doing statistics is really easy. Below is a list of commands that you will need to do any kind of statistics in RStudio.

3.1 Summary Statistics

  • summary(X) - Summary statistics such as mean,median,mode and quartiles for a variable.

summary(X) Min. 1st Qu. Median Mean 3rd Qu. Max. -3.0360 -0.8855 -0.2475 -0.2382 0.3345 3.

  • mean(X,na.rm=TRUE) - Produces the mean of the variable. Removes missing observations.

mean(X,na.rm=TRUE) [1] -0.

  • sd(X,na.rm=TRUE) - Produces the standard deviation of the variable. Removes missing observations.

sd(X,na.rm=TRUE) [1] 0.

3.2 Regression

  • lm(Y ∼ X) - Runs a regression of Y on X where Y is your dependent variable and X is your independent variable. You need to save your model in R’s memory first and can get the regression coefficients and other info you need by using the summary() command. For example, for simple regression:

model1 = lm(Y~X) summary(model1)

Call: lm(formula = Y ~ X)

Residuals: Min 1Q Median 3Q Max

model2 = lm(Y~X) plot(X,Y) abline(model2)

3.3 Hypothesis Testing

  • t.test(X,Y) - Performs a t-test of means between two variables X and Y for the hypothesis H 0 : μX = μY. Gives t-statistic, p-value and 95% confidence interval. Example:

t.test(X,Y)

Welch Two Sample t-test

data: X and Y t = -0.2212, df = 193.652, p-value = 0. alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -0.3231116 0.

sample estimates: mean of x mean of y -0.2382041 -0.

3.4 Graphics and Plots

  • hist(X) - Will produce a histogram of the variable X.

hist(X)

  • plot(X,Y) - Will produce a scatterplot of the variables X and Y with X on the x-axis and Y on the y-axis.

plot(X,Y)