Chapter 5 Basics of R Practice

Lander's chapter 4 - Basics of R

Data come in many types, and R is well equipped to handle them. In addition to basic calculations, R can handle numeric, character and time-based data. One of the nicer parts of working with R, although one that requires a different way of thinking about programming, is vectorization. This allows operating on multiple elements in a vector simultaneously, which leads to faster and more mathematical code.

#basic math
1+1
## [1] 2
1+2+3
## [1] 6
3*7*2
## [1] 42
4/2
## [1] 2
4/3
## [1] 1.333333
4*6+5
## [1] 29
(4*6)+5
## [1] 29
4*(6+5)
## [1] 44
#variables
#variable assignment
x<-2; y=5;
x;y
## [1] 2
## [1] 5
a<-b<-7; a; b
## [1] 7
## [1] 7
assign("j", 4)
j
## [1] 4
#removing variables
j
## [1] 4
rm(j)

#data type
class(x)
## [1] "numeric"
is.numeric(x)
## [1] TRUE
i<-5L
i
## [1] 5
is.integer(x)
## [1] FALSE
is.integer(i)
## [1] TRUE
class(4L*0.28)
## [1] "numeric"
class(4L/2L)
## [1] "numeric"
#character data
x<-"data"
y<-factor("data")
y
## [1] data
## Levels: data
x; nchar(x)
## [1] "data"
## [1] 4
nchar(452)
## [1] 3
#dates
date1 <- as.Date("2012-06-28")
date1
## [1] "2012-06-28"
as.numeric(date1) # January 1, 1970
## [1] 15519
date2 <- as.POSIXct("2012-06-28 17:42")
date2
## [1] "2012-06-28 17:42:00 EDT"
class(date2)
## [1] "POSIXct" "POSIXt"
#logical
TRUE * 5
## [1] 5
FALSE * 5
## [1] 0
T
## [1] TRUE
class(T)
## [1] "logical"
2==3
## [1] FALSE
2!=3
## [1] TRUE
2<3
## [1] TRUE
2<=3
## [1] TRUE
"data"<"stats"
## [1] TRUE
#vector
x<-c(1,2,3,4,5,6,7,8,9,10)
x*3
##  [1]  3  6  9 12 15 18 21 24 27 30
x+2
##  [1]  3  4  5  6  7  8  9 10 11 12
x-3
##  [1] -2 -1  0  1  2  3  4  5  6  7
x/4
##  [1] 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 2.50
x^2
##  [1]   1   4   9  16  25  36  49  64  81 100
sqrt(x)
##  [1] 1.000000 1.414214 1.732051 2.000000 2.236068 2.449490 2.645751 2.828427 3.000000 3.162278
1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
10:1
##  [1] 10  9  8  7  6  5  4  3  2  1
-2:3
## [1] -2 -1  0  1  2  3
5:-7
##  [1]  5  4  3  2  1  0 -1 -2 -3 -4 -5 -6 -7
(x<-1:10)
##  [1]  1  2  3  4  5  6  7  8  9 10
(y<--5:4)
##  [1] -5 -4 -3 -2 -1  0  1  2  3  4
x+y
##  [1] -4 -2  0  2  4  6  8 10 12 14
x/y
##  [1] -0.2 -0.5 -1.0 -2.0 -5.0  Inf  7.0  4.0  3.0  2.5
length(x)
## [1] 10
x+c(1,2)
##  [1]  2  4  4  6  6  8  8 10 10 12
x+c(1,2,3)
## Warning in x + c(1, 2, 3): longer object length is not a multiple of shorter object length
##  [1]  2  4  6  5  7  9  8 10 12 11
x==5
##  [1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
(x<-10:1); (y<--4:5)
##  [1] 10  9  8  7  6  5  4  3  2  1
##  [1] -4 -3 -2 -1  0  1  2  3  4  5
all(x<y)
## [1] FALSE
any(x<y)
## [1] TRUE
nchar(y)
##  [1] 2 2 2 2 1 1 1 1 1 1
nchar(c("apple", "orange"))
## [1] 5 6
x[1]
## [1] 10
x[1:2]
## [1] 10  9
x[c(1,3)]
## [1] 10  8
#create a vector with names
w<-1:3
names(w) <- c("a", "b", "c")
w
## a b c 
## 1 2 3
w["a"]; w[1]
## a 
## 1
## a 
## 1
#factor vectors
q2 <- c("usa", "china", "uk", "france", "russia")
class(q2)
## [1] "character"
(q2<-as.factor(q2))
## [1] usa    china  uk     france russia
## Levels: china france russia uk usa
as.numeric(q2)
## [1] 5 1 4 2 3
factor(x=c("high school", "college", "masters", "doctorate"),
       levels = c("high school", "college", "masters", "doctorate"),
       ordered = T)
## [1] high school college     masters     doctorate  
## Levels: high school < college < masters < doctorate
#functions
mean(x)
## [1] 5.5
#missing data
(z<-c(1,2,NA, 8, 3, NA, 3))
## [1]  1  2 NA  8  3 NA  3
is.na(z)
## [1] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
#NULL
z<-c(1,NULL,3)
z
## [1] 1 3
d<-NULL
is.null(d)
## [1] TRUE