Files

216 lines
3.7 KiB
R
Raw Permalink Normal View History

2026-02-12 17:16:36 +09:00
## 기초 명령어 실습 ##
1+1
1:10
dd = c(1:10)
## c() -> 같은 종류의 요소(numeric, int, float등)만 있을때
## list() -> 다양한 종류의 요소가 들어올때
dd[-10]
## 기본유형 ##
x1 = 3 # numeric
x2 = "Love is choice" # character
x3 = FALSE # logical
x4 = 3-2i # complex
x5 = '123' # character
x6 = 'false' # character
x7 = 'FLASE' # character
str(x1)
### 문자 - 정수 - 논리
### 결측값은 95%까지
### 정규분포는 30번이상한것으로 해야한다
xx1 <- c(1:100)
xx1
is.na(xx1)
table(is.na(xx1))
x1 = c(1,'love',TRUE,2+3i)
x1
2026-02-19 15:50:45 +09:00
v1 = c(27,35,47,41)
mode(v1)
is.numeric(v1)
is.na(v1)
table(is.na(v1))
v2 = c(27,35,47,NA,55)
mode(v2)
is.numeric(v2)
is.na(v2)
table(is.na(v2))
length(v1)
length(v2)
names(v1)
names(v1) = c('kim','lee','park','choi')
names(v1)
## Indexing ## 대괄호 == [ ] => 추출해라
v1
v1[1]
v1[1:3]
v1[-4]
v1[c(2,4)]
v1 = 1:3
v2 = 4:6
v3 = v1+v2 # can add list
v1 = 1:3
v2 = 1:6
v3 = v1+v2 # reusable add
v3
### 데이터거래사
## Factor, ordered Factor (등급을 정할 수 있는)
gender = c('m','f','f','m','f','f')
gender_factor = factor(gender)
levels(gender_factor)
gender_factor2 = factor(gender, levels = c('m','f'), labels = c('남자','여자'))
gender_factor2
gender_factor3 = factor(gender,ordered = TRUE)
gender_factor3
## Matrix ##
v1 = 1:3
v2 = 4:6
m1 = rbind(v1,v2)
m2 = cbind(v1,v2)
v1
v2
m1
m2
m1[2,1] == 8
m1[2,1] = 8
m1[,3]
is.na(m1)
table(is.na(m1))
m1[1,1] = NA
m1[2,3] = NA
table(is.na(m1))
str(m1)
m3 = matrix(1:4, nrow = 2, ncol = 2)
m4 = matrix(1:4, nrow = 2, ncol = 2, byrow = TRUE)
m3
m4[2,1] = NA
## array ##
a1 = array(1:10, dim=10)
a2 = array(1:10, dim=c(2, 5))
a3 = array(1:10, dim=c(3, 3, 4))
a1
a2
a3[,,2][3,3] = 100
a3
## DataFrame ##
id = 1:5
age = c(29, 32, 47, 35, 23)
gender = c('f','m','m','f','f')
height = c(163, 177, 172, 157, 169)
DF1 = data.frame(id, age, gender, height)
DF2 = data.frame(id, age, gender, height, stringsAsFactors=FALSE)
DF3 = data.frame(id, age, gender, height, stringsAsFactors=TRUE)
DF1
DF2
DF3
View(DF3)
str(DF3)
DF4 = DF3[-1]
DF4
View(DF4)
str(DF4)
## 기술통계량 ##
2026-02-20 15:16:04 +09:00
library(psych) # describe()명령어 호출 라이브러리
2026-02-19 15:50:45 +09:00
describe(DF4)
2026-02-20 15:16:04 +09:00
rownames(DF1) = paste('Edge',1:5,sep='_')
rownames(DF1)
colnames(DF1) = paste(c('아이디','나이','성별','키'))
colnames(DF1)
dim(DF1)
searchpaths()
## read text data ##
k200 = read.csv(file = './data/k100.csv',header = TRUE)
k300 = read.csv(file = './data/ebook.csv',header = TRUE)
tsv100 = read.table(file = './data/survey_tab.txt', header = TRUE, fileEncoding = 'CP949')
## data handling ##
library(ggplot2)
head(diamonds)
View(diamonds)
2026-03-20 17:04:54 +09:00
tail(diamonds)
2026-02-20 15:16:04 +09:00
head(Titanic)
View(Titanic)
2026-03-20 17:04:54 +09:00
tail(Titanic)
2026-02-20 15:16:04 +09:00
## data structure ##
str(diamonds)
str(Titanic)
## indexing ##
diamonds[ , 2]
diamonds[ , 2, drop=FALSE]
xx = diamonds[ , c(2, 3, 7)] # 2,3,7번째 값 추출
ss = diamonds[,7] # 7번째 값 추출
describe(diamonds) # 기술통계량
diamonds[ , 7:10]
diamonds[ , seq(from=2, to=10, by=2)]
diamonds[ , 'cut']
diamonds[ , c('cut', 'price')]
## 행추출하기 ##
a = diamonds[diamonds$cut == 'Fair', ]
a
b = diamonds[diamonds$price >= 18000, ] # 다이아몬드의 가격을 기준으로 추출
b
c = diamonds[(diamonds$cut == 'Fair') & (diamonds$price >= 18000), ]
d = diamonds[(diamonds$cut == 'Fair') | (diamonds$price >= 18000), ]
2026-03-20 17:04:54 +09:00
diamonds$xyz = diamonds$x + diamonds$y + diamonds$z
View(diamonds)
diamonds$means = diamonds$xyz/3
kkk = diamonds[-c(10,20,30),]
str(kkk)
kkk1 = diamonds[-c(100:200),]
str(kkk1)
kkk2 = diamonds[-seq(from=1, to=length(diamonds), by=10),]
str(kkk2)