a <- 10 # a는 10입니다.
b <- 5 # b는 5입니다.
c <- "animal" # c는 animal입니다.
a
## [1] 10
b
## [1] 5
c
## [1] "animal"
a + b # 더하기
## [1] 15
a - b # 빼기
## [1] 5
a / b # 나누기
## [1] 2
a * b # 곱하기
## [1] 50
a^2 # 제곱
## [1] 100
sqrt(a^2) # 루트
## [1] 10
a == b # a는 b와 같다.
## [1] FALSE
a != b # a는 b와 같지 않다.
## [1] TRUE
a > b # a는 b보다 크다.
## [1] TRUE
a < b # a는 b보다 작다.
## [1] FALSE
a >= b # a는 b보다 크거나 같다.
## [1] TRUE
a <= b # a는 b보다 작거나 같다.
## [1] FALSE
a <- 1:10
a
## [1] 1 2 3 4 5 6 7 8 9 10
b <- c(1, 2, 3, 4, 5, 10)
b
## [1] 1 2 3 4 5 10
c <- c("Konkuk", "University")
c
## [1] "Konkuk" "University"
example <- data.frame(name = c("철수", "영희", "건이"), sex = c("Male", "Female", "Male"), age = c(29, 40, 33), height = c(175, 163, 168))
example
str(example)
## 'data.frame': 3 obs. of 4 variables:
## $ name : Factor w/ 3 levels "건이","영희",..: 3 2 1
## $ sex : Factor w/ 2 levels "Female","Male": 2 1 2
## $ age : num 29 40 33
## $ height: num 175 163 168
x <- example$height # example 데이터 중 height 데이터만 추출
x
## [1] 175 163 168
example[2, 3]
## [1] 40
example[3, 3]
## [1] 33
example[, 3]
## [1] 29 40 33
example[1, ]
example[-1, ]
example[c(2, 3), ]
sum(x) # 합계
## [1] 506
mean(x) # 평균
## [1] 168.6667
median(x) # 중간값
## [1] 168
var(x) # 분산
## [1] 36.33333
sd(x) # 표준편차
## [1] 6.027714
max(x) # 최대값
## [1] 175
min(x) # 최소값
## [1] 163
range(x) # 범위
## [1] 163 175
install.packages("dplyr")
install.packages("ggplot2")
install.packages("readxl")
update.packages(ask = FALSE)
library(dplyr)
library(ggplot2)
read.csv("dataframe.csv")
readxl::read_excel("dataframe.xlsx")
write.csv(example, "test.txt", row.names = FALSE)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
readxl::read_excel("example_df.xlsx")
df <- readxl::read_excel("example_df.xlsx")
filter(df, Name == "Kim")
filter(df, weight > 500)
filter(df, wgrade != "C")
filter(df, qgrade != "3")
filter(df, qgrade == "1++" & wgrade == "A") # and
filter(df, qgrade == "1++" | wgrade == "A") # or
df$month %>% mean()
## [1] 32.06825
mean(df$month)
## [1] 32.06825
group_by(df, Name) %>% summarise(avg = mean(month))
group_by(df, Name) %>% summarise(month = mean(month), mean_weight = mean(weight), max_weight = max(weight))
group_by(df, qgrade) %>% summarise(month = mean(month))
library(ggplot2)
g <- ggplot(df, aes(x = month, y = weight))
g
g + geom_point()
h <- ggplot(df, aes(qgrade))
h + geom_bar()
h + geom_bar() + scale_x_discrete(limits = c("3", "2", "1", "1+", "1++")) # 순서 지정하기
h + geom_bar(width = 0.5, fill = "red") + scale_x_discrete(limits = c("3", "2", "1", "1+", "1++")) # 너비 및 색 지정
g <- ggplot(df, aes(month))
g + geom_density(kernel = "gaussian")
filter(df, month < 40) %>% ggplot(aes(month)) + geom_density(kernel = "gaussian") # 40개월 미만의 개체의 분포도
filter(df, Name == "Kim") %>% ggplot(aes(wgrade)) + geom_bar(width = 0.4, fill = "#81BEF7") + coord_flip() # 김씨네 육량등급
본 수업을 통해 기본적인 R 사용법을 배워보았습니다. 각자가 다뤄야 하는 데이터의 종류와 성격이 다르겠지만 기본적으로 데이터를 가공하고-분석하고-해석하는 일련의 과정들은 다르지 않을 것입니다. 지금은 비록 기본만 배웠지만 각자가 더 공부해 분석 및 보고서 작성과정에 들이는 시간을 줄일 수 있길 바래봅니다.