Lab 1 22.7
Lab 1 22.7
Introduction to R
Why use it?
Setting up R Environment
Data Types
File Handling,
Plotting and Graphic features
Packages
What is ?
“R is a freely available language and
environment for statistical computing and
graphics”
View help,
plots & files;
manage
packages
R console
Naming Convention
must start with a letter (A-Z or a-z)
case-sensitive
mydata different from MyData
x<-c(1,2,3,4,5,6,7)
x<-c(1:7)
x<-1:4
R as a calculator
> 5 + (6 + 7) * pi^2
[1] 133.3049
> log(exp(1))
[1] 1
1.0
[1] 5
0.5
sin(seq(0, 2 * pi, length = 100))
> sqrt(2)
0.0
[1] 1.414214
-0.5
> seq(0, 5, length=6)
[1] 0 1 2 3 4 5 -1.0
0 20 40 60 80 100
Index
“Not a number”
> log(c(0, 1, 2))
[1] -Inf 0.0000000 0.6931472
> 0/0
[1] NaN
Basic (atomic) data types
Logical Character
> x <- T; y <- F > a <- "1"; b <- 1
> x; y > a; b
[1] TRUE [1] "1"
[1] 1
[1] FALSE
> a <- "character"
> b <- "a"; c <- a
Numerical > a; b; c
> a <- 5; b <- [1] "character"
sqrt(2) [1] "a"
> a; b [1] "character"
[1] 5
[1] 1.414214
R Program to Take Input From User
readline() function to take input from the user (terminal).
This function will return a single element character vector.
Example
my.name <- readline(prompt="Enter name: ")
my.age <- readline(prompt="Enter age: ")
It may be created:
Using rep():
> rep("bye",2)
[1] "bye" "bye"
> 5>3
[1] TRUE
> x=1:5
> x
[1] 1 2 3 4 5
> x<3
[1] TRUE TRUE FALSE FALSE FALSE
Manipulation of Vectors
Our vector: x=c(100,101,102,103)
> length(y)
[1] 5
> mean(y + z)
[1] 4.2
Manipulation of Vectors –
Cont.
Mydata <- c(2,3.5,-0.2)
Vector c=“concatenate”)
Colors <- c("Red","Green","Red")
Character vector
x1 <- 25:30
> x1
[1] 25 26 27 28 29 30 Number sequences
> Colors[2]
[1] "Green" One element
> x1[3:5]
[1] 27 28 29 Various elements
Manipulation of Vectors –
Cont.
> Mydata
[1] 2 3.5 -0.2