R programming for Data Science by Roger
v1 = c(6,5,4,3,2,1) .. to define a vector you use c for it.
v1 = c(6,5,4,3,2,1)
v2 = c(10,9,8,7,6,5)
v3 = v1 + v2
v3
[1] 16 14 12 10 8 6
v1 <- c(1,2,3,4)
v2 <- c(2,3)
v3 <- v1+v2
3 5 5 7
The plus operator knows how to add two vectrs
cbind is used to combine columns of vectors.
Vectors: to access elements in a vector (1 base)
a <- c(1,2,3)
a[1] -----> this is how you access the first element.
x <- a[[1]] --> this means a[1] ----> means exact same thing.
Refer to elements of a vector using subscripts. a[c(2,4)] # 2nd and 4th elements of vector.
> a <- c(6,7,8)
> a[c(1,2,3,4,5)]
[1] 6 7 8 NA NA
_________________________________
> a <- c(6,7,8)
> a[c(1,2,3,4,5)]
[1] 6 7 8 NA NA
> z = a[c(1,2,3,4,5)]
> is.na(z)
[1] FALSE FALSE FALSE TRUE TRUE
_____________________
> a = matrix(1:20, 5, 4)
> View(a)
1 | 1 | 6 | 11 | 16 |
---|---|---|---|---|
2 | 2 | 7 | 12 | 17 |
3 | 3 | 8 | 13 | 18 |
4 | 4 | 9 | 14 | 19 |
5 | 5 | 10 | 15 | 20 |
x = a[2,]
>x = 2 , 7, 12, 17
x = a[,]
returns entire matrix
> x = sum(a[,3])
x is now the sum of the entire 3rd column
_____________
Data Frame:
matrix whith same types in columns but dfferent types accorss a row.
> d <- c(1,2,3,4)
> e <- c("red", "white", "red", NA)
> f <- c(TRUE,TRUE,TRUE,FALSE)
> mydata <- data.frame(d,e,f)
> View(mydata)
dplyr
tidyverse
sparklyr - frontend like zepplin for spark cluster
ggplot2
you need to have these 3 packages.
________________
if you use ggplot2 .. you can plot to a webpage.. its a live chart.