### 9주차
## 1)
lgm <- function(x, y){
res <- 0
for (i in 1:max(x, y)){
if (x %% i == 0 & y %% i == 0){
res <- i
}
}
return(res)
}
lgm(10,8)
lgm(10,20)
## 2)
maxmin <- function(x){
ma <- max(x)
mi <- min(x)
return(list(max=ma, min=mi))
}
v1 <- c(7,1,2,8,9)
result <- maxmin(v1)
result$max
result$min
## 3)
weight <- c(69,50,55,71,89,64,59,70,71,80)
max(weight)
min(weight)
which.max(weight)
which.min(weight)
which(weight>=61 & weight<=69)
w <- which(weight<=60)
weight2 <- weight[w]
weight2
### 10주차
## 1)
gender <- c('F','F','F','M','M','F','F','F','M','M')
table(gender)
## 2)
par(mfrow=c(1,2))
gt <- table(gender)
barplot(gt, main='gender_bar')
pie(gt, main = 'gender_pie')
## 3)
score <- c(90,85,73,80,85,65,78,50,68,96)
mean(score)
median(score)
mean(score, trim=0.1)
sd(score)
## 4)
par(mfrow = c(1,2))
hist(score, main = 'score_his', xlab = '성적', ylab = '과목의 수', las = 2)
boxplot(score, main='score_box')
## 5)
point <- as.numeric(iris$Species)
color <- c('red','green','blue')
plot(iris$Sepal.Length, iris$Sepal.Width, main = 'iris_plot', col = color, pch = point)
## 6)
year <- c(20144,20151,20152,20153,20154,20161,20162,20163,20164,20171,20172,20173)
men <- c(73.9,73.1,74.4,74.2,73.5,73,74.2,74.5,73.8,73.1,74.5,74.2)
women <- c(51.4,50.5,52.4,52.4,51.9,50.9,52.6,52.7,52.2,51.5,53.2,53.1)
plot(year, women, type='l',main='economic Participation',col='red',xlab='year',ylab='Percentage', ylim=c(50,80))
lines(year, men, col = 'blue', ylim=c(50,80))
### 11주차
## 1)
ds <- state.x77
ds[2,3] <- NA
ds[3,1] <- NA
ds[2,4] <- NA
ds[4,3] <- NA
for (i in 1:ncol(ds)){
this_na <- is.na(ds[,i])
cat(colnames(ds)[i], ' ', sum(this_na), '\n')
}
## 2)
sum(rowSums(is.na(ds))>0)
ds.new <- ds[complete.cases(ds),]
head(ds.new)
## 3)
df <- data.frame(state.x77)
out_val <- boxplot.stats(df$Population)$out
df$Population[df$Population %in% out_val] <- NA
df_2 <- df[complete.cases(df),]
rownames(df)
rownames(df_2)
## 4)
state.x77[order(state.x77[, "Population"], decreasing = F),]
state.x77[order(state.x77[,'Income'], decreasing = T),]
## 5)
mt_g <- split(mtcars, mtcars$gear)
mt_g
mt_wt <- subset(mtcars, wt > 1.5 & wt < 3.0)
mt_wt
## 6)
set.seed(100)
st_sam <- sample(1:nrow(state.x77), size = 20)
st_other <- state.x77[-st_sam,]
nrow(st_other)
## 7)
authors <- data.frame(surname = c('Twein','Venables','Tierney','McNeil'), nationality = c('US','Australia','US','UK')
)
books <- data.frame(name = c('Venables','Tierney','Ripley','McNiel'), title=c('Modern Applied Statistics', 'LISP-STAT', 'Spatil Statistics','Interactive Data Analysis'))
merge(authors, books, by.x = c('surname'), by.y=c('name'))
### 12주차
## 2)
symbols(swiss$Fertility, swiss$Agriculture, circles = swiss$Education, bg = 'lightgreen', xlab = 'Fertility', ylab = 'Agriculture')
text(swiss$Fertility, swiss$Agriculture, rownames(swiss), col = 'darkblue')
## 3)
library(ggplot2)
ggplot(mtcars, aes(x=gear)) + geom_bar() + labs(x = '기어의 수', y = '빈도수')
## 4)
ggplot(mtcars, aes(x=mpg, y=wt, col=gear)) + geom_point(size = 2)
## 5)
library(Rtsne)
dup <- which(duplicated(swiss))
dup
tsne <- Rtsne(swiss, dims = 2, perplexity = 10)
swiss.tsne <- as.data.frame(tsne$Y)
ggplot(swiss.tsne, aes(x=V1, y=V2)) + geom_point(size = 3)