Escolar Documentos
Profissional Documentos
Cultura Documentos
Programming
Beginning with R
R can be started by double-clicking the
desktop shortcut icon or by going to Start-
>Program-> R
The program will open with the window
This is the starting point for all that is to
come
The R startup window/ console or
command window
Rs data structure include vectors,
matrices, arrays, data frames and lists
R is an interpreted language; users
typically access it through a command
line interpreter
Simple R commands
# Basic mathematical operations
>3 + 4
>5 * 5
>12 / 3
>5^5
>log(2)
>log10(2)
Getting Data into R
# Vector
## Most basic object in R
## Contains elements of the same class
## Can be: character, numeric, integer, complex,
logical(True/False)
# Create a vector
##Concatenating Data with the c Function
##c function creates a single vector of length 4
>v=c(1,3,5,7)
>v
# List
## (Vector with different class of objects)
>l=c("Blue", 2, 5, "Red")
>l
#Matrix
# Create a matrix
>m=matrix(1:6,2,3)
>m
## Matrix creation is column-wise
# Create a matrix from a vector
>m2=matrix(1:6)
# Then add dimensionality
>dim(m2)=c(2,3)
>m2
# Create a matrix by binding columns or rows
>x=1:6
>y=5:10
>cbind(x,y) # by column
>rbind(x,y) # by row
# Check the attributes
>attributes(m)
# Call a particular cell in a matrix
>m
>m[1,2]
# Dataframes
## Different than matrices => can store different classes of objects
## Usually created with data.frame()
# Create a dataframe
>d=data.frame(subjectID=1:5,gender=c("M","F","F","M","F"),score=c(8,3,6,5,5))
>d
# Number of rows
>nrow(d)
# Number of columns
>ncol(d)
# Check the attributes
>attributes(d)
# Call a particular cell in a dataframe
>d[2,1]
>d[1,2]
# Display dataframe
>View(d)
# Edit dataframe
>edit(d)
# Getting help on a function
>?functionname
# Download and install packages
>install.packages("psych") ## Need to specify CRAN the 1st time (India)
# Load package
>library(psych)
Thank You