Introductory code for R

In this post we are going to document some introductory commands in the R programming language. We are going to build sequentially these commands and attempt to reach more advanced functions as time progresses. First, here are the steps for downloading R and the R Studio, the user-friendly environment for R, a set of integrated tools, designed to help the user get acquainted with the language easier.

Install R, RStudio, and R Commander in Windows

  1. Download R from (click on “Download R for Windows” > “base” > “Download R 2.x.x for Windows”)
  2. Install R. Leave all default settings in the installation options
  3. Download RStudio from and install it. Leave all default settings in the installation options
  4. Open RStudio

Run the following code, by copying and pasting to the R Studio interface:

#The symbol # implies commenting and it is not part of the coding-it functions as an explanatory sentence or section of the code presented. Enjoy programming!

#Introductory commands in R
#Up and Running with R

2 + 2 # Basic math

1:100 # Prints numbers 1 to 100 across several lines

print(“Hello World!”) # Prints “Hello World” in console

# Variables
x <- 1:5 # Put the numbers 1-5 in the variable x
x # Displays the values in x
y <- c(6, 7, 8, 9, 10) # Puts the numbers 6-10 in y
y # Displays y
x + y # Adds corresponding elements in x and y
x * 2 # Multiplies each element in x by 2

ls() # List objects

2017-09-24 22_20_18-RStudio.png
A view of the local R Studio instance with the code documented in this post

# Inserting data to R

# R converts missing to “NA”
# Don’t have to specify delimiters for missing data
# because CSV means “comma separated values”
# “header = T” means the first line is a header
# This first one will not work because of the backslashes
social_data <- read.csv(“C:\Users\james\Downloads\R Training\Exercise Files\Ch02\02_04\social_network.csv”, header = T)
# Need to either double up the backslashes…
social_data <- read.csv(“C:\\Users\\james\\Downloads\\R Training\\Exercise Files\\Ch02\\02_04\\social_network.csv”, header = T)
# Or replace with forward slashes
social_data <- read.csv(“C:/Users/james/Downloads/R Training/Exercise Files/Ch02/02_04/social_network.csv”, header = T)

str(social_data) #for structure


Leave a Reply

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

You are commenting using your 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