The Power of R

Here I would like to share Five Steps in Twelve lines of R code to draw some plots, histograms, boxplot and pie chart (so many stuff!).  It is super easy and convenient! I am sure you will be impressed by the R power, just like I do.

Environment: R Studio
Data Source: Rdatasets (A great place to download existing data for practice).

I will be using the SATGPA dataset to see if there is any correlation between students’ GPA and SAT score. Here we go:

Step 1. Set up our work directory:

# Get work directory:
getwd()

#Set work directory:
setwd(“/Users/user/Projects/R-data”)

Screen Shot 2017-08-03 at 11.03.23 AMStep 2. Read some data and check the data

#Read CSV data into a variable data1:
data1 <- read.csv(“csv/Stat2Data/SATGPA.csv”, header = TRUE)

#Output the data1:
data1

# Display the structure of data1
str(data1)

 

Step 3. Draw some plots

# Plot the GPA variable of data1
plot(data1$GPA)

# Plot the GPA variable against the MathSAT Screen Shot 2017-08-03 at 11.05.31 AMvariable
plot(data1$GPA, data1$MathSAT)

# Plot the GPA variable against the VerbalSAT variable
plot(data1$GPA, data1$VerbalSAT)

Step 4. How about some histograms!

# Create a histogram for GPAScreen Shot 2017-08-03 at 11.06.51 AM
hist(data1$GPA)

# Create a histogram for rating, with 20 bins
hist(data1$GPA, breaks = 20)

Step 5. boxplot and pie Chart!

# Create a boxplot of the GPA variable
boxplot(data1$GPA)Screen Shot 2017-08-03 at 11.07.44 AM

# Create a pie chart of the table of counts of the GPA
pie(table(data1$GPA))

Are you ready to learn some R now? 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s