Introduction To R
Introduction To R
R is a programming language and software environment for statistical computing and graphics
supported by the R Foundation for Statistical Computing.
History
R is an implementation of the S programming language combined with lexical scoping semantics inspired
by Scheme.[11] S was created by John Chambers while at Bell Labs.
R was created by Ross Ihaka and Robert Gentleman[13] at the University of Auckland, New Zealand, and is
currently developed by the R Development Core Team, of which Chambers is a member. R is named partly after
the first names of the first two R authors and partly as a play on the name of S.[14]
Editor Window
Documentation results
Command Line
z = x-y
>z
[1] 0.0000000 0.3010300 0.4771213 0.6020600 0.6989700 0.7781513 0.8450980 0.9030900 0.9542425 1.0000000
Copyright 2017 : Anish Roychowdhury
Preliminary Data Assignment and Math Operators contd.
#Rounding Numbers
x = 123.456
# normal rounding 2 decimal
places
z = round(x,digits = 2) > z [1]
123.46
# flooring > z [1]
z = floor(x)
123
# ceiling
z = ceiling(x) > z [1]
124
# truncating decimal part
z = trunc(x) > z [1]
123
> First_and_Second
[1] 12.0 4.0 4.0 6.0 9.0 3.0 2.5 3.0 3.5 4.0 4.5 5.0
Copyright 2017 : Anish Roychowdhury
More on Vectors
# repeat a vector 3 times
vec3 <- c(0,0,7) > Rvec3
Rvec3 <-rep(vec3,times=3) [1] 0 0 7 0 0 7 0 0 7
# What did Player 1 score in the matches player 1 won ? > P1_win_scores
P1_win_scores <- Player_1[Player_1_win] [1] 10 34 78
1 2 3
𝐴= # Extract 2nd row 3rd column
4 5 6 Command continuation
> A23
A23 <- A[2, 3]
[1] 7
A = matrix(
+ c(2, 4, 3, 1, 5, 7), # the data elements
+ nrow=2, # number of rows # Extract 2nd row as a vector
+ ncol=3, # number of columns ARow2Vec <- A[2, ] # the 2nd row
+ byrow = TRUE) # fill matrix by rows > ARow2Vec
[1] 1 5 7
> A # print the matrix
[,1] [,2] [,3] # Extracting a sub matrix
[1,] 2 4 3 A2by2 <- A[1:2,1:2]
[2,] 1 5 7 > A2by2
[,1] [,2]
[1,] 2 4
[2,] 1 5
Copyright 2017 Anish Roychowdhury
Data Frames
A data frame is used for storing data tables. It is a list of vectors of equal length. For
example, the following variable df is a data frame containing three vectors n, s, b.
n <- c(2, 3, 5)
s <- c("aa", "bb", "cc")
b <- c(TRUE, FALSE, TRUE)
df <- data.frame(n, s, b) # df is a data frame
How the data frame would look – Each vector becomes a column in the data frame
n s b df n s b
2 aa TRUE 1 2 aa TRUE
3 bb FALSE 2 3 bb FALSE
5 cc TRUE 3 5 cc TRUE
> n = c(2, 3, 5)
> s = c("aa", "bb", "cc", "dd", "ee")
> b = c(TRUE, FALSE, TRUE, FALSE, FALSE)
> x = list(n, s, b, 3) # x contains copies of n, s, b
[[1]] [1] 2 3 5
[[2]] [1] "aa" "bb" "cc" "dd" "ee"
[[3]] [1] TRUE FALSE TRUE FALSE FALSE
[[4]] [1] 3
Var1 <- 5
Initialize the vector with 5 repeats of '10' and then 5 repeats of '20'
# Initialize the first column to 1,s the 2nd col to 2's and the 3rd col to 3's
for (i in 1:5){
df_3col_5row[i,] <- c(1,2,3)
} Copyright 2017 Anish Roychowdhury
List Initialization concepts
Create List column names