This is a very ^2 basic introduction to R.
The purpose of this presentation is to prepare you with all that you have to know about fundamentals of using R to operate data frames, which you can easily get by importing data from relational database table or csv/text file.
5. R Very Basics
A bunch of functions and data types to work with data.
- Not like C/java/scala/python. Don’t built products with it.
- It works with Matrices, or super/sub types of it:
- Vectors, Matrices, Data Frames… etc
- So, Think in tabular ways.
- All the operations based upon matrices.
9. Matrix
> A = matrix(
c(2, 4, 3, 1, 5, 7), # the data elements
nrow=2,
# number of rows
ncol=3,
# number of columns
byrow = TRUE)
>A
# fill matrix by rows
# print the matrix
[,1] [,2] [,3]
[1,]
2
4
3
[2,]
1
5
7
11. List
A list is a generic vector containing other objects.
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
[http://www.r-tutor.com/r-introduction/list]
12. Data Frame
Basically like a mysql table.
Building one:
n = c(2, 3, 5)
s = c("aa", "bb", "cc")
b = c(TRUE, FALSE, TRUE)
df = data.frame(n, s, b)
14. Data Frame 3
Subsetting data frames:
df[1]
df[“s”]
df[c("s", "b")]
subset(df, n<=3)
Adding two data frames together:
combined <- rbind(df, df2)
data <- read.csv("/Path/To/Ur/CSV/R_Example.csv",
header=TRUE)
[http://www.r-bloggers.com/select-operations-on-r-dataframes/]
15. Data Frame 4
We wanna explore data:
select * from df where b=true
can be translated into
df_sub1 <- df[df$b==TRUE,]
Then, the holy grail: something like sql join
k = c('the','X','Men')
s = c("aa", "bb", "cc")
z = c(3.1415961, 124235243, 2309)
df2 = data.frame(k, s, z)
merged <- merge(df, df2, 's') #tada!!!
16. QA and Onward
Who is up to a R study group?
- Plotting (ggplot)
- etc…