---
title: "base r learning note"
author: "JIASHU MIAO"
date: "2019/2/4"
output: html_document
---
```{r}
library("dplyr")
library("readxl")
```
```{r}
?mean # help of function 
str(iris)
class(iris)
typeof(iris)
par(mfrow=c(2,2))
data(mtcars)
x <- c(2,4,6,8,10,NA)
seq(2,3,by = 0.1)
rep(1:2, times=3)
rep(1:2,each=3)
sort(x,decreasing = TRUE)
table(x)
unique(x)
rev(x)
mean(x, na.rm = F)
x <- na.omit(x)
```
```{r}
x
x[4]
x[-c(1,2)]
x[-(2:4)]

 #x[‘apple’]
# element with name apple

 
# for and while , if else loop

for (i in 1:4){
   j <- i + 10
print(j)
 }
```

```{r}
while (i<5 & i > 0 ) {
  
  i= i+25
  print(i)
}
i
# 没搞明白

```
```{r}
i= 1
if (i >3){
  print("yes")
} else {
  print("no")}

# global environment



square <- function(x){
  x <- 8
  square <- x*x
  return(square)
  e
}
print(square()) # 这时候 square变成了一个function 括号里面可以添加参数

```
```{r}
#read.table() read.csv 
mydat <- read_xlsx("/Users/MichaelMiao/Documents/GitHub/R-scripts-/Acumen_Data_Analysis_Exercise.xlsx",sheet = "Data")


# as.logical()
# as.array()
# as.factor()
# as.character()
# as.numeric()

x <- c(2,4,6,8,10,12)
y <- x +c(1,2)
y
log(x)
exp(x)
max(x)
min(x)
round(x,5)
sign(x)
cor(x,y)
sum(x)
mean(x)
median(x)
quantile(x)
var(x)
sd(x)
rank(x)
summary(lm(y~x))

```

```{r}
#ls()
#rm(x)
# rm(list = ls())

mat1 <- matrix(1:9,nrow = 3,byrow = F)
mat1
mat2 <- matrix(1:9,nrow = 3,byrow = T)
mat2
# print(c(mat1,mat2)) 是不对的
mat1 * mat2
mat1 %*% mat2
#solve(mat1,mat2) how to use solve func. 
```
```{r}
l <- list(x=1:5,y=c("a","b"))
l[[2]]
l[1]
l[[2]][2]
l['y']



```

```{r}

df <- data.frame(x=1:3,y=letters[1:3]) # 牛逼
df
nrow(df) #ncol, dim, cbind, rbind

```
```{r}
#strings 
paste(x,y,sep = "")
paste(x,collapse = "")
grepl(x,x) # find the patter in the expression 
grep(x,x)
#  look at the grep function and the grelp function

# repalce the matcheds in x with a string
# gsub(pattern,replace, x)
# toupper / tolower 大写小切换

nchar(x)
x
# 12 有俩characters

cut(x,breaks = 4 ) #Turn a numeric vector into a factor by ‘cutting’ into sections. ????????


# check ggplot2 and lubridate package

x <- 10
if(x > 0)
{
print("This is Positive number")
}

```

```{r}
x <- 10
if(x > 0)
{
print("This is Positive number")
}

```

```{r}
# statement function 
x <- -10
if(x >= 0)
{
print("This is Non-negative number")
} else {
print("This is Negative number")
}

```

note for stats 140sl 

ca take the exam anywhere 
no json file 
probably not spss/stata
<!-- prob excel / Rdata -->
there is a bunch of dataset at kaggle   
for example the gradudate admission datasets (should not be a probably in after 101c)
also review the stats knowledge for modelling, validation etc. traning
review the code in 101c
archive.ics.uci.eud/ml/datasets.html
reviw about the ggplot2 
trunc() function

```{r}
library(haven)
library(dplyr)
#summarise_all(mydata,funs(n(),mean(.,na.rm = T)))
# drop is not as mean or median 
mydata
summarise_all(mydata,funs(median))
#head(mydata$vs)
# sample_frac(): Randomly select a fraction of rows
dd <- c(1.5,1.6,1.8)
# dd
# dd <- trunc(dd)
# 不懂区别
#dd <- floor(dd)
# dd <- ceiling(dd)
# dd trunc 总是去 0


```
```{r}
x <- 5
if (x < 0) 
{
 print("This is Negative number")
} else if (x > 0) {
print("This is Positive number")
} else {
print("This is Zero")
}

for(i in 1:5)
{
for(j in 1:2)
{
print(i*j);
}
}
# R for loop with break statement
 

x <- 1:5
for (i in x) {
    if (i == 3){
        break
    }
    print(i)
}
i <- 1
while (i <=6) {
print(i*i)
i = i+1
}

```
```{r}
# Syntax for Replicate Function in R:
# 
# rep(value,number_of_times)
# rep(sequence,each,number_of_times)

rep(3:4, time = 3
    )
rep(3:4, each = 3)

```
```{r}
sapply(mydata,mode)


# Create two lists.
list1 <- list(1,2,3)
list2 <- list("Jan","Feb","Mar")
 
# Merge the two lists.
merged.list <- c(list1,list2)
 
# Print the merged list.
print(merged.list)

merged.list[[2]][1]

```
```{r}
df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Oven", 3), rep("Television", 3)))
df1
# data frame 2
df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("California", 2), rep("Texas", 1)))
df2
df<-merge(x=df1,y=df2,by="CustomerId")
df<-merge(x=df1,y=df2,by="CustomerId",all=TRUE)
df<-merge(x=df1,y=df2,by="CustomerId",all.x=TRUE)
df<-merge(x=df1,y=df2,by="CustomerId",all.y=TRUE)
df <- merge(df1,df2,by = "CustomerId", all.x = T)

# Cross join in R: A Cross Join (also sometimes known as a Cartesian Join) results in every row of one table being joined to every row of another table


df<-merge(x = df1, y = df2, by = NULL)

```
```{r}
# with function in R
 df <- data.frame(a=1:5,b=2:6)
 df
 df$c<-with(df, c <- a + b)
 df
 
 # within function in R
 
within(df, {c <- a + b; df;} ) ##花括号干嘛的
```
```{r}
# Generate sequence from 0 to 20 with length.out=5 with R seq function
 
seq(from=0, to=20, length.out=5)


```


# apply function 
## an aggregating function, like for example the mean, or the sum (that return a number or scalar);
other transforming or sub-setting functions;
and other vectorized functions, which return more complex structures like list, vectors, matrices and arrays.
```{r}
#Apply(x,1,sum) 
Age<-c(56,34,67,33,25,28)
Weight<-c(78,67,56,44,56,89)
Height<-c(165, 171,167,167,166,181)
 
BMI_df<-data.frame(Age,Weight,Height)
BMI_df
# row wise sum up of dataframe using apply function in R
apply(BMI_df,1,sum)
apply(BMI_df,2,sum)
```
Where the first Argument X is a data frame or matrix
Second argument 1 indicated Processing along rows .if it is 2 then it indicated processing along the columns
Third Argument is some aggregate function like sum, mean etc or some other user defined functions.



- 2. lapply function in R:

lapply function takes list, vector or Data frame  as input and returns only list as output.

We will be using same dataframe for depicting example on lapply function
```{r}
lapply(BMI_df, function(BMI_df) BMI_df/2)
s <- function(x)x^2
s(3)
ss <- lapply(BMI_df, mean)
ss <- ss  %>%  as.data.frame()

```


- 3. Sapply function in R

sapply function takes list, vector or Data frame  as input. It is similar to lapply function but returns only vector as output.

We will be using same dataframe for depicting example on sapply function

```{r}
ss <- sapply(BMI_df, function(BMI_df) BMI_df/2) %>% as.data.frame()
ss
ss <- sapply(BMI_df, function(BMI_df) BMI_df/2)
ss
class(ss)
random <- c("This","is","random","vector")
sapply(random,nchar)
```


- 4 mapply function in R:

mapply is a multivariate version of sapply. mapply applies FUN to the first elements of each (…) argument, the second elements, the third elements, and so on.

i.e. For when you have several data structures (e.g. vectors, lists) and you want to apply a function to the 1st elements of each, and then the 2nd elements of each, etc., coercing the result to a vector/array as in sapply

This is multivariate in the sense that your function must accept multiple arguments.

```{r}
# mapply function in R
mapply(sum, 1:4, 1:4, 1:4)

# mapply sums up all the first elements(1+1+1) ,sums up all the
# 
# second elements(2+2+2) and so on so the result will be


# !!!!

# mapply function in R
mapply(rep,1:4,1:4)
mapply(FUN = rep, 1:8, 8)

```

- 5. tapply function in R:

For when you want to apply a function to subsets of a vector and the subsets are defined by some other vector, usually a factor.

Lets go back to the famous iris data. Species is a factor with 3 values namely Setosa, versicolor and virginica. If we want to find the mean of sepal length of these 3 species(subsets). we can use tapply function
```{r}
# tapply function in R
attach(iris)
# mean sepal length by species
tapply(iris$Sepal.Length, Species, mean)
# irst argument of tapply function takes the vector for which we need to perform the function. second argument is a vector by which we need to perform the function and third argument is the function, here it is mean. So the output will be
data_original <- read_excel("/Users/MichaelMiao/Documents/career/accument\ data\ excercise/Acumen_Data_Analysis_Exercise.xlsx", sheet = "Data")
dataNEW2 <- subset(dataNEW1, dataNEW1$Age >= 18 & dataNEW1$Age <= 63 )
# summary(dataNEW2)
dataNEW2 <- na.omit(dataNEW2)
attach(dataNEW2)
tapply(dataNEW2$`Health Score`, Race, length)

```

- rapply function in R is nothing but recursive apply, as the name suggests it is used to apply a function to all elements of a list recursively.
- rapply function is applied even for the sublists and output will be


```{r}
# rapply function in R
x=list(3,list(4,5),6,list(7,list(8,9)))
str(x)
rapply(x,function(x) x^2,class=c("numeric"))

```

- vapply function in R is similar to sapply, but has a pre-specified type of return value, so it can be safer (and sometimes faster) to use.

1
2
# vapply function in R
vapply(1:5, sqrt, 1i)
output will be

   [1] 1.000000 0i      1.414214 0i     1.732051 0i         2.000000 0i         2.236068 0i
   
  
- sub() and gsub() function in R
sub() and gsub() function in R are replacement functions, which replaces the occurrence of a substring with other substring.

sub() Function in R replaces the first instance of a substring
gsub() function in R replaces all the instances of a substring

```{r}
# sub function in R only the first occurrence 
 
mysentence <- "England is Beautiful. England is not the part of EU"
sub("England", "UK", mysentence)

# gsub function in R global 全部替换
 
mysentence <- "England is Beautiful. England is not the part of EU"
gsub("England", "UK", mysentence)
```

- Aggregate() Function in R Splits the data into subsets, computes summary statistics for each subsets and returns the result in a group by form. Aggregate function in R is similar to group by in SQL.

- aggregate(x, by, FUN, simplify = TRUE)

```{r}
# Aggregate function in R with mean summary statistics
agg_mean <- aggregate(iris[,1:4],by=list(iris$Species),FUN=mean, na.rm=TRUE)
agg_mean


# Aggregate function in R with sum summary statistics # na.rm !!!
agg_sum<-aggregate(iris[,1:4],by=list(iris$Species),FUN=sum, na.rm=TRUE)
agg_sum

```
```{r}
# return whether the value is matched or not
 
df1 %in% df2
v1 <- c(2,5,6,3,7)
v2 <- c(15,16,7,3,2,7,5)
# match function in R with nomatch=0
 
match(v1,v2,nomatch=0)
v2[match(v1,v2,nomatch = )]
match(v2,v1,incomparables=2)
```


- Intersection of two data frames in R can be easily achieved by using merge() function. Lets see with an example. First lets create two data frames
```{r}
#  Create two data frames
 

x <- c(1:4)
y <- c(2:7)
intersect(x,y)

df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Oven", 3), rep("Television", 3)))
df2 = data.frame(CustomerId = c(4:7), Product = c(rep("Television", 2), rep("Air conditioner", 2)))

merge(df1,df2, all = T )
merge(df1,df2, all = T, by = "Product" ,sort=T )


df1["Product"]

```

```{r}
# rowmeans

rowMeans(iris[,1:3])

mydata13[,-c(1,2)]

```


- rank() function in R returns the ranks of the values in a vector. rank function in R also handles Ties and missing values in several ways.

```{r}
# NAs are ranked first

1
2
x <- c(2,7,1,-17,NA,Inf,35,21)
rank(x,na.last = FALSE) #NA FIRST position rank of vectors

# NAs are neglectled by rank function

1
2
x <- c(2,7,1,-17,NA,Inf,35,21)
rank(x,na.last = NA) #NA removed

x <- c(2,7,1,-17,NA,Inf,35,21)
rank(x,na.last = "keep") #NA is kept with rank NA



```

- Which function in R, returns the indices of the logical object when it is TRUE. In other words, which() function in R returns the position of value when it satisfies the specified condition.

```{r}



length(which(is.na(data_original$Race)))
data_original[is.na(data_original$Race),]
colSums(is.na(data_original))
rs <- rowSums(is.na(data_original))
any(rs > 2) 
  
rs <- which(!data_original$Race == 1 & 2 & 3) # bu hao
length(rs)
#sort() only vector
# order() df ok

# order(data_original, method = "Race")

# df[order(df$State,df$Mortality.Rate,df$Hospital.Name),]

identical(2,2)

df
attach(df)
df[order(a,b,c, decreasing = T),]
df$c <- df$c[order(c,decreasing = F)]
df
df[order(c, decreasing = T),]
df$c <- df$c %>% as.factor(.)
class(df$c)
aggregate(a,by=list(df$c),FUN = sum) #???

prop.table(table(data$Race[data$Quarter==1])) ## see the propo



```

Generalized Linear Models
Generalized linear models are fit using the glm( ) function. The form of the glm function is

glm(formula, family=familytype(link=linkfunction), data=)

Family	Default Link Function
binomial	(link = "logit")
gaussian	(link = "identity")
Gamma	(link = "inverse")
inverse.gaussian	(link = "1/mu^2")
poisson	(link = "log")
quasi	(link = "identity", variance = "constant")
quasibinomial	(link = "logit")
quasipoisson	(link = "log")























