x <- c(1,2,3)
x
## [1] 1 2 3
library(readr)
sample1 <- read_csv("sample1_en_csv.csv")
## Rows: 3 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Name, Gender
## dbl (2): ID, Math
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
sample1
## # A tibble: 3 × 4
## ID Name Gender Math
## <dbl> <chr> <chr> <dbl>
## 1 1 James Male 91.1
## 2 2 Robert Male 95.3
## 3 3 Mary Female 93.5
sample1 <- read.csv2("sample1_en_csv2.csv")
sample1
## ID Name Gender Math
## 1 1 James Male 91.1
## 2 2 Robert Male 95.3
## 3 3 Mary Female 93.5
sample1 <- read_tsv("sample1_en_tsv.tsv")
## Rows: 3 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (2): Name, Gender
## dbl (2): ID, Math
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
sample1
## # A tibble: 3 × 4
## ID Name Gender Math
## <dbl> <chr> <chr> <dbl>
## 1 1 James Male 91.1
## 2 2 Robert Male 95.3
## 3 3 Mary Female 93.5
sample1 <- read_delim("sample1_en_delim.txt", delim="|")
## Rows: 3 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "|"
## chr (2): Name, Gender
## dbl (2): ID, Math
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
sample1
## # A tibble: 3 × 4
## ID Name Gender Math
## <dbl> <chr> <chr> <dbl>
## 1 1 James Male 91.1
## 2 2 Robert Male 95.3
## 3 3 Mary Female 93.5
sample1 <- read.csv("sample1_en_csv_nonames.csv")
sample1
## X1 James Male X91.1
## 1 2 Robert Male 95.3
## 2 3 Mary Female 93.5
sample1 <- read_csv("sample1_en_csv_nonames.csv", col_names = FALSE)
## Rows: 3 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): X2, X3
## dbl (2): X1, X4
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
sample1
## # A tibble: 3 × 4
## X1 X2 X3 X4
## <dbl> <chr> <chr> <dbl>
## 1 1 James Male 91.1
## 2 2 Robert Male 95.3
## 3 3 Mary Female 93.5
sample1 <- read_csv("sample1_en_csv_nonames.csv",
col_names = c("ID", "Name", "Gender", "math"))
## Rows: 3 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Name, Gender
## dbl (2): ID, math
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
sample1
## # A tibble: 3 × 4
## ID Name Gender math
## <dbl> <chr> <chr> <dbl>
## 1 1 James Male 91.1
## 2 2 Robert Male 95.3
## 3 3 Mary Female 93.5
sample1 <- read_csv("sample1_en_csv_skip.csv")
## New names:
## Rows: 5 Columns: 4
## ── Column specification
## ────────────────────────────────────────────────────────
## Delimiter: "," chr (4): 2021, ...2, ...3, ...4
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...2`
## • `` -> `...3`
## • `` -> `...4`
sample1
## # A tibble: 5 × 4
## `2021` ...2 ...3 ...4
## <chr> <chr> <chr> <chr>
## 1 math scores <NA> <NA> <NA>
## 2 ID Name Gender Math
## 3 1 James Male 91.1
## 4 2 Robert Male 95.3
## 5 3 Mary Female 93.5
sample1 <- read_csv("sample1_en_csv_skip.csv", skip = 2)
## Rows: 3 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Name, Gender
## dbl (2): ID, Math
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
sample1
## # A tibble: 3 × 4
## ID Name Gender Math
## <dbl> <chr> <chr> <dbl>
## 1 1 James Male 91.1
## 2 2 Robert Male 95.3
## 3 3 Mary Female 93.5
sample1 <- read.csv("sample1_en_csv.csv")
sample1
## ID Name Gender Math
## 1 1 James Male 91.1
## 2 2 Robert Male 95.3
## 3 3 Mary Female 93.5
sample1 <- read_csv("sample1_en_csv.csv", col_types = "icfd")
sample1
## # A tibble: 3 × 4
## ID Name Gender Math
## <int> <chr> <fct> <dbl>
## 1 1 James Male 91.1
## 2 2 Robert Male 95.3
## 3 3 Mary Female 93.5
sample1 <- read_csv(c("sample1_en_csv.csv", "sample2_en_csv.csv"))
## Rows: 6 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Name, Gender
## dbl (2): ID, Math
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
sample1
## # A tibble: 6 × 4
## ID Name Gender Math
## <dbl> <chr> <chr> <dbl>
## 1 1 James Male 91.1
## 2 2 Robert Male 95.3
## 3 3 Mary Female 93.5
## 4 4 Richard Male 96.3
## 5 5 Linda Female 92.8
## 6 6 Jessica Female 94.2
sample1 <- read_csv(c("sample1_en_csv.csv", "sample2_en_csv.csv"), id = "path")
## Rows: 6 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Name, Gender
## dbl (2): ID, Math
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
sample1
## # A tibble: 6 × 5
## path ID Name Gender Math
## <chr> <dbl> <chr> <chr> <dbl>
## 1 sample1_en_csv.csv 1 James Male 91.1
## 2 sample1_en_csv.csv 2 Robert Male 95.3
## 3 sample1_en_csv.csv 3 Mary Female 93.5
## 4 sample2_en_csv.csv 4 Richard Male 96.3
## 5 sample2_en_csv.csv 5 Linda Female 92.8
## 6 sample2_en_csv.csv 6 Jessica Female 94.2
library(readr)
file_names <- list.files("./samples", full.names = TRUE)
file_names
## [1] "./samples/sample1_en_csv.csv" "./samples/sample2_en_csv.csv"
all_samples <- read_csv(file_names)
## Rows: 6 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Name, Gender
## dbl (2): ID, Math
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
all_samples
## # A tibble: 6 × 4
## ID Name Gender Math
## <dbl> <chr> <chr> <dbl>
## 1 1 James Male 91.1
## 2 2 Robert Male 95.3
## 3 3 Mary Female 93.5
## 4 4 Richard Male 96.3
## 5 5 Linda Female 92.8
## 6 6 Jessica Female 94.2
library(readxl)
sample1 <- read_excel("sample3_en_excel.xlsx")
sample1
## # A tibble: 3 × 4
## ID Name Gender Math
## <dbl> <chr> <chr> <dbl>
## 1 1 James Male 91.1
## 2 2 Robert Male 95.3
## 3 3 Mary Female 93.5
sample2 <- read_excel("sample3_en_excel.xlsx", sheet = 2)
sample2
## # A tibble: 3 × 4
## ID Name Gender Math
## <dbl> <chr> <chr> <dbl>
## 1 4 Richard Male 96.3
## 2 5 Linda Female 92.8
## 3 6 Jessica Female 94.2
sample2 <- read_excel("sample3_en_excel.xlsx", sheet = "2021")
sample2
## # A tibble: 3 × 4
## ID Name Gender Math
## <dbl> <chr> <chr> <dbl>
## 1 4 Richard Male 96.3
## 2 5 Linda Female 92.8
## 3 6 Jessica Female 94.2
logical_vector <- c(TRUE, FALSE, T, F)
integer_vector <- c(1L, 6L, 5L)
double_vector <- c(1, 2.5, 3.8)
character_vector <- c("apple", "orange")
typeof(c(1,2,3))
## [1] "double"
length(c(1,2,3))
## [1] 3
attributes(c(1,2,3))
## NULL
a <- c(x=1, y=2, z=3)
a
## x y z
## 1 2 3
attributes(a)
## $names
## [1] "x" "y" "z"
attr(a, "names")
## [1] "x" "y" "z"
names(a) <- c("l", "m", "n")
a
## l m n
## 1 2 3
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
attributes(mtcars)
## $names
## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
## [11] "carb"
##
## $row.names
## [1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
## [4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant"
## [7] "Duster 360" "Merc 240D" "Merc 230"
## [10] "Merc 280" "Merc 280C" "Merc 450SE"
## [13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood"
## [16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128"
## [19] "Honda Civic" "Toyota Corolla" "Toyota Corona"
## [22] "Dodge Challenger" "AMC Javelin" "Camaro Z28"
## [25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2"
## [28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino"
## [31] "Maserati Bora" "Volvo 142E"
##
## $class
## [1] "data.frame"
v <- c(4,7,23.5,76.2,80, "rrt")
v
## [1] "4" "7" "23.5" "76.2" "80" "rrt"
typeof(v)
## [1] "character"
v <- c(1, TRUE, TRUE, FALSE)
v
## [1] 1 1 1 0
typeof(v)
## [1] "double"
a <- c(2,5,1,4,6)
a>3
## [1] FALSE TRUE FALSE TRUE TRUE
sum(a>3)
## [1] 3
a <- c(1,2,3)
a
## [1] 1 2 3
b <- as.character(a)
b
## [1] "1" "2" "3"
a <- c(4,6,NA,2)
a
## [1] 4 6 NA 2
is.na(a)
## [1] FALSE FALSE TRUE FALSE
!is.na(a)
## [1] TRUE TRUE FALSE TRUE
a <- c(1,2,3,4,5,6)
a
## [1] 1 2 3 4 5 6
a <- c(1,2,3)
b <- c(4,5,6)
c <- c(a,b)
c
## [1] 1 2 3 4 5 6
seq(from = 1, to = 10, by=1)
## [1] 1 2 3 4 5 6 7 8 9 10
seq(1,10,1)
## [1] 1 2 3 4 5 6 7 8 9 10
seq(1,10)
## [1] 1 2 3 4 5 6 7 8 9 10
1:10
## [1] 1 2 3 4 5 6 7 8 9 10
10:5
## [1] 10 9 8 7 6 5
rep(1,3)
## [1] 1 1 1
rep(c(1,2), 3)
## [1] 1 2 1 2 1 2
rep(c(1,2), each = 3)
## [1] 1 1 1 2 2 2
x <- c(2,3,4,5,6,7)
x[c(3,1)]
## [1] 4 2
x <- c(2,3,4,5,6,7)
x[-c(3,1)]
## [1] 3 5 6 7
x[c(TRUE, TRUE, FALSE, FALSE, TRUE, TRUE)]
## [1] 2 3 6 7
x <- c(2,3,4,5,6,7)
x>3
## [1] FALSE FALSE TRUE TRUE TRUE TRUE
x[x>3]
## [1] 4 5 6 7
x[x>3 & x <5]
## [1] 4
: 앞의 원소가 뒤에 존재하는지 여부 확인해서 존재하면 TRUE, 없으면 FALSE (앞의 원소를 판별하는 것)
c(1,2,3) %in% c(2,3,4,5,6)
## [1] FALSE TRUE TRUE
a <- c(1,2,3,4,5)
a
## [1] 1 2 3 4 5
a[3] <- 100
a
## [1] 1 2 100 4 5
ex.결측값을 999로 수정
a <- c(1,2,3,NA,5,6, NA)
a
## [1] 1 2 3 NA 5 6 NA
is.na(a)
## [1] FALSE FALSE FALSE TRUE FALSE FALSE TRUE
a[is.na(a)] <- 999
a
## [1] 1 2 3 999 5 6 999
v <- c(1,4,25)
sqrt(v)
## [1] 1 2 5
v <- c(1,2,3)
v^2
## [1] 1 4 9
v1 <- c(1,2,3,4)
v2 <- c(10,20)
v1 + v2
## [1] 11 22 13 24
v1 <- c(1,2,3,4)
v2 <- c(10,20)
v1 + v2
## [1] 11 22 13 24
*유용한 함수
sort(c(5,6,4))
## [1] 4 5 6
sort(c(5,6,4), decreasing = TRUE)
## [1] 6 5 4
a <- c("male", "female", "female", "male", "male")
table(a)
## a
## female male
## 2 3
a <- c("male", "female", "female", "male", "male")
unique(a)
## [1] "male" "female"
a <- c(1,2,1,2,3,3,1,2)
unique(a)
## [1] 1 2 3
a <- matrix(1:6, ncol=3, nrow=2)
a
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
attributes(a)
## $dim
## [1] 2 3
dim(a)
## [1] 2 3
nrow(a)
## [1] 2
ncol(a)
## [1] 3
colnames(a) <- c("a", "b", "c")
a
## a b c
## [1,] 1 3 5
## [2,] 2 4 6
rownames(a) <- c("Lee", "Kim")
a
## a b c
## Lee 1 3 5
## Kim 2 4 6
a <- matrix(1:9, nrow=3)
rownames(a) <- c("홍길동","김철수","이영희")
colnames(a) <- c("국어", "영어", "수학")
a
## 국어 영어 수학
## 홍길동 1 4 7
## 김철수 2 5 8
## 이영희 3 6 9
a[1,2]
## [1] 4
a>3
## 국어 영어 수학
## 홍길동 FALSE TRUE TRUE
## 김철수 FALSE TRUE TRUE
## 이영희 FALSE TRUE TRUE
a[a>3] <- NA
a
## 국어 영어 수학
## 홍길동 1 NA NA
## 김철수 2 NA NA
## 이영희 3 NA NA
a <- matrix(1:9, nrow=3)
rownames(a) <- c("홍길동","김철수","이영희")
colnames(a) <- c("국어", "영어", "수학")
rbind(a, a)
## 국어 영어 수학
## 홍길동 1 4 7
## 김철수 2 5 8
## 이영희 3 6 9
## 홍길동 1 4 7
## 김철수 2 5 8
## 이영희 3 6 9
cbind(a,a)
## 국어 영어 수학 국어 영어 수학
## 홍길동 1 4 7 1 4 7
## 김철수 2 5 8 2 5 8
## 이영희 3 6 9 3 6 9
: 레벨(level)과 클래스(class)라는 속성(attributes, 메타데이터) 을 가진 정수형벡터(integer vector)
f1 <- factor(c("m", "f", "m", "m", "f"))
f1
## [1] m f m m f
## Levels: f m
typeof(f1)
## [1] "integer"
attributes(f1)
## $levels
## [1] "f" "m"
##
## $class
## [1] "factor"
levels(f1)
## [1] "f" "m"
levels(f1) <- c("female", "male")
f1
## [1] male female male male female
## Levels: female male
f1 <- factor(c("m", "f", "m", "m", "f"))
f1
## [1] m f m m f
## Levels: f m
table(f1)
## f1
## f m
## 2 3
f1 <- factor(c("m", "f", "m", "m", "f"), levels=c("m", "f"))
f1
## [1] m f m m f
## Levels: m f
table(f1)
## f1
## m f
## 3 2
f1 <- factor(c("high", "low", "medium", "high"),
levels=c("low", "medium", "high"),
ordered=TRUE)
f1
## [1] high low medium high
## Levels: low < medium < high
min(f1)
## [1] low
## Levels: low < medium < high
x <- c("Agree", "Disagree", "Neutral", "Agree", "Agree", "Strongly disagree", "Neutral")
f1 <- factor(x, levels = c("Strongly disagree", "Disagree", "Neutral", "Agree", "Strongly agree"),
ordered = TRUE)
f1
## [1] Agree Disagree Neutral Agree
## [5] Agree Strongly disagree Neutral
## 5 Levels: Strongly disagree < Disagree < Neutral < ... < Strongly agree
f1[1] > f1[2]
## [1] TRUE
x <- list(1:6, "a", c(TRUE, TRUE, FALSE), c(1.2, 3.3, 4.6, 6.6))
x
## [[1]]
## [1] 1 2 3 4 5 6
##
## [[2]]
## [1] "a"
##
## [[3]]
## [1] TRUE TRUE FALSE
##
## [[4]]
## [1] 1.2 3.3 4.6 6.6
str(x)
## List of 4
## $ : int [1:6] 1 2 3 4 5 6
## $ : chr "a"
## $ : logi [1:3] TRUE TRUE FALSE
## $ : num [1:4] 1.2 3.3 4.6 6.6
typeof(x)
## [1] "list"
my.list <- list(stud.id=34453,
stud.name="John",
stud.marks=c(14.3, 12, 15, 19))
my.list
## $stud.id
## [1] 34453
##
## $stud.name
## [1] "John"
##
## $stud.marks
## [1] 14.3 12.0 15.0 19.0
예시1) [] : 리스트의 원소를 리스트로 반환함
my.list[1]
## $stud.id
## [1] 34453
typeof(my.list[1])
## [1] "list"
예시2) [[]] : 리스트의 원소를 원소의 자료형으로 반환함
my.list[[1]]
## [1] 34453
typeof(my.list[[1]])
## [1] "double"
예시3) $ : 리스트의 원소를 원소의 자료형으로 반환함
my.list$stud.id
## [1] 34453
typeof(my.list$stud.id)
## [1] "double"
my.list <- list(stud.id=34453,
stud.name="John",
stud.marks=c(14.3, 12, 15, 19))
my.list[[3]]
## [1] 14.3 12.0 15.0 19.0
my.list[[3]][2]
## [1] 12
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
?mtcars
## httpd 도움말 서버를 시작합니다 ... 완료
fit <- lm(mpg ~ disp + cyl, data = mtcars)
fit
##
## Call:
## lm(formula = mpg ~ disp + cyl, data = mtcars)
##
## Coefficients:
## (Intercept) disp cyl
## 34.66099 -0.02058 -1.58728
typeof(fit)
## [1] "list"
str(fit)
## List of 12
## $ coefficients : Named num [1:3] 34.661 -0.0206 -1.5873
## ..- attr(*, "names")= chr [1:3] "(Intercept)" "disp" "cyl"
## $ residuals : Named num [1:32] -0.844 -0.844 -3.289 1.573 4.147 ...
## ..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
## $ effects : Named num [1:32] -113.65 -28.44 -6.81 2.04 4.06 ...
## ..- attr(*, "names")= chr [1:32] "(Intercept)" "disp" "cyl" "" ...
## $ rank : int 3
## $ fitted.values: Named num [1:32] 21.8 21.8 26.1 19.8 14.6 ...
## ..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
## $ assign : int [1:3] 0 1 2
## $ qr :List of 5
## ..$ qr : num [1:32, 1:3] -5.657 0.177 0.177 0.177 0.177 ...
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
## .. .. ..$ : chr [1:3] "(Intercept)" "disp" "cyl"
## .. ..- attr(*, "assign")= int [1:3] 0 1 2
## ..$ qraux: num [1:3] 1.18 1.09 1.19
## ..$ pivot: int [1:3] 1 2 3
## ..$ tol : num 1e-07
## ..$ rank : int 3
## ..- attr(*, "class")= chr "qr"
## $ df.residual : int 29
## $ xlevels : Named list()
## $ call : language lm(formula = mpg ~ disp + cyl, data = mtcars)
## $ terms :Classes 'terms', 'formula' language mpg ~ disp + cyl
## .. ..- attr(*, "variables")= language list(mpg, disp, cyl)
## .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
## .. .. ..- attr(*, "dimnames")=List of 2
## .. .. .. ..$ : chr [1:3] "mpg" "disp" "cyl"
## .. .. .. ..$ : chr [1:2] "disp" "cyl"
## .. ..- attr(*, "term.labels")= chr [1:2] "disp" "cyl"
## .. ..- attr(*, "order")= int [1:2] 1 1
## .. ..- attr(*, "intercept")= int 1
## .. ..- attr(*, "response")= int 1
## .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## .. ..- attr(*, "predvars")= language list(mpg, disp, cyl)
## .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
## .. .. ..- attr(*, "names")= chr [1:3] "mpg" "disp" "cyl"
## $ model :'data.frame': 32 obs. of 3 variables:
## ..$ mpg : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## ..$ disp: num [1:32] 160 160 108 258 360 ...
## ..$ cyl : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
## ..- attr(*, "terms")=Classes 'terms', 'formula' language mpg ~ disp + cyl
## .. .. ..- attr(*, "variables")= language list(mpg, disp, cyl)
## .. .. ..- attr(*, "factors")= int [1:3, 1:2] 0 1 0 0 0 1
## .. .. .. ..- attr(*, "dimnames")=List of 2
## .. .. .. .. ..$ : chr [1:3] "mpg" "disp" "cyl"
## .. .. .. .. ..$ : chr [1:2] "disp" "cyl"
## .. .. ..- attr(*, "term.labels")= chr [1:2] "disp" "cyl"
## .. .. ..- attr(*, "order")= int [1:2] 1 1
## .. .. ..- attr(*, "intercept")= int 1
## .. .. ..- attr(*, "response")= int 1
## .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## .. .. ..- attr(*, "predvars")= language list(mpg, disp, cyl)
## .. .. ..- attr(*, "dataClasses")= Named chr [1:3] "numeric" "numeric" "numeric"
## .. .. .. ..- attr(*, "names")= chr [1:3] "mpg" "disp" "cyl"
## - attr(*, "class")= chr "lm"
fit$coefficients
## (Intercept) disp cyl
## 34.66099474 -0.02058363 -1.58727681
fit$coefficients[1]
## (Intercept)
## 34.66099
my.df <- data.frame(stu.id = c(1,2,3),
stu.name = c("홍길동", "김철수", "이영희"),
stu.score = c(93.2, 89.7, 95.8)
)
my.df
## stu.id stu.name stu.score
## 1 1 홍길동 93.2
## 2 2 김철수 89.7
## 3 3 이영희 95.8
my.df[2]
## stu.name
## 1 홍길동
## 2 김철수
## 3 이영희
typeof(my.df[2])
## [1] "list"
my.df[[2]]
## [1] "홍길동" "김철수" "이영희"
typeof(my.df[[2]])
## [1] "character"
my.df$stu.name
## [1] "홍길동" "김철수" "이영희"
typeof(my.df$stu.name)
## [1] "character"
my.df[1,2]
## [1] "홍길동"
my.df[1,]
## stu.id stu.name stu.score
## 1 1 홍길동 93.2
my.df[ ,2]
## [1] "홍길동" "김철수" "이영희"
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
typeof(mtcars)
## [1] "list"
str(mtcars)
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
mtcars[1]
## mpg
## Mazda RX4 21.0
## Mazda RX4 Wag 21.0
## Datsun 710 22.8
## Hornet 4 Drive 21.4
## Hornet Sportabout 18.7
## Valiant 18.1
## Duster 360 14.3
## Merc 240D 24.4
## Merc 230 22.8
## Merc 280 19.2
## Merc 280C 17.8
## Merc 450SE 16.4
## Merc 450SL 17.3
## Merc 450SLC 15.2
## Cadillac Fleetwood 10.4
## Lincoln Continental 10.4
## Chrysler Imperial 14.7
## Fiat 128 32.4
## Honda Civic 30.4
## Toyota Corolla 33.9
## Toyota Corona 21.5
## Dodge Challenger 15.5
## AMC Javelin 15.2
## Camaro Z28 13.3
## Pontiac Firebird 19.2
## Fiat X1-9 27.3
## Porsche 914-2 26.0
## Lotus Europa 30.4
## Ford Pantera L 15.8
## Ferrari Dino 19.7
## Maserati Bora 15.0
## Volvo 142E 21.4
typeof(mtcars[1])
## [1] "list"
mtcars[[1]]
## [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4
typeof(mtcars[[1]])
## [1] "double"
mtcars$mpg
## [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4
typeof(mtcars$mpg)
## [1] "double"
mtcars[,1]
## [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4
typeof(mtcars[,1])
## [1] "double"
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
mean(mtcars$mpg)
## [1] 20.09062
mean(mtcars$cyl)
## [1] 6.1875
lapply(mtcars, mean)
## $mpg
## [1] 20.09062
##
## $cyl
## [1] 6.1875
##
## $disp
## [1] 230.7219
##
## $hp
## [1] 146.6875
##
## $drat
## [1] 3.596563
##
## $wt
## [1] 3.21725
##
## $qsec
## [1] 17.84875
##
## $vs
## [1] 0.4375
##
## $am
## [1] 0.40625
##
## $gear
## [1] 3.6875
##
## $carb
## [1] 2.8125
sapply(mtcars, mean)
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
my_vector <- c(1,2,3)
mean(my_vector)
## [1] 2
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ purrr 1.0.2
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
my_mean <- function(x) {
my_sum <- sum(x)
my_length <- length(x)
return(my_sum/my_length)
}
a <- c(1,2,3)
my_mean(a)
## [1] 2
mean(a)
## [1] 2
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
(mtcars$mpg - mean(mtcars$mpg))/sd(mtcars$mpg)
## [1] 0.15088482 0.15088482 0.44954345 0.21725341 -0.23073453 -0.33028740
## [7] -0.96078893 0.71501778 0.44954345 -0.14777380 -0.38006384 -0.61235388
## [13] -0.46302456 -0.81145962 -1.60788262 -1.60788262 -0.89442035 2.04238943
## [19] 1.71054652 2.29127162 0.23384555 -0.76168319 -0.81145962 -1.12671039
## [25] -0.14777380 1.19619000 0.98049211 1.71054652 -0.71190675 -0.06481307
## [31] -0.84464392 0.21725341
–> 코드의 중복을 줄이기 위해 반복되어 사용되는 코드를 함수로 정의하면 함수의 호출을 통해 반복되는 작업을 쉽게 수행할 수 있음.
my_zscore <- function(x) {
return((x-mean(x))/sd(x))
}
my_zscore(mtcars$mpg)
## [1] 0.15088482 0.15088482 0.44954345 0.21725341 -0.23073453 -0.33028740
## [7] -0.96078893 0.71501778 0.44954345 -0.14777380 -0.38006384 -0.61235388
## [13] -0.46302456 -0.81145962 -1.60788262 -1.60788262 -0.89442035 2.04238943
## [19] 1.71054652 2.29127162 0.23384555 -0.76168319 -0.81145962 -1.12671039
## [25] -0.14777380 1.19619000 0.98049211 1.71054652 -0.71190675 -0.06481307
## [31] -0.84464392 0.21725341
–> 이미 z-score를 계산할 수 있는 scale() 함수가 있음
scale(mtcars$mpg)
## [,1]
## [1,] 0.15088482
## [2,] 0.15088482
## [3,] 0.44954345
## [4,] 0.21725341
## [5,] -0.23073453
## [6,] -0.33028740
## [7,] -0.96078893
## [8,] 0.71501778
## [9,] 0.44954345
## [10,] -0.14777380
## [11,] -0.38006384
## [12,] -0.61235388
## [13,] -0.46302456
## [14,] -0.81145962
## [15,] -1.60788262
## [16,] -1.60788262
## [17,] -0.89442035
## [18,] 2.04238943
## [19,] 1.71054652
## [20,] 2.29127162
## [21,] 0.23384555
## [22,] -0.76168319
## [23,] -0.81145962
## [24,] -1.12671039
## [25,] -0.14777380
## [26,] 1.19619000
## [27,] 0.98049211
## [28,] 1.71054652
## [29,] -0.71190675
## [30,] -0.06481307
## [31,] -0.84464392
## [32,] 0.21725341
## attr(,"scaled:center")
## [1] 20.09062
## attr(,"scaled:scale")
## [1] 6.026948
for(i in 1:3) {
print(i^2)
}
## [1] 1
## [1] 4
## [1] 9
output <- vector("double", 3)
output
## [1] 0 0 0
for (i in 1:3) {
output[i] <- i^2
}
output
## [1] 1 4 9
output <- vector("list", 11)
for(i in 1:11) {
output[[i]] <- my_zscore(mtcars[[i]])
}
output
## [[1]]
## [1] 0.15088482 0.15088482 0.44954345 0.21725341 -0.23073453 -0.33028740
## [7] -0.96078893 0.71501778 0.44954345 -0.14777380 -0.38006384 -0.61235388
## [13] -0.46302456 -0.81145962 -1.60788262 -1.60788262 -0.89442035 2.04238943
## [19] 1.71054652 2.29127162 0.23384555 -0.76168319 -0.81145962 -1.12671039
## [25] -0.14777380 1.19619000 0.98049211 1.71054652 -0.71190675 -0.06481307
## [31] -0.84464392 0.21725341
##
## [[2]]
## [1] -0.1049878 -0.1049878 -1.2248578 -0.1049878 1.0148821 -0.1049878
## [7] 1.0148821 -1.2248578 -1.2248578 -0.1049878 -0.1049878 1.0148821
## [13] 1.0148821 1.0148821 1.0148821 1.0148821 1.0148821 -1.2248578
## [19] -1.2248578 -1.2248578 -1.2248578 1.0148821 1.0148821 1.0148821
## [25] 1.0148821 -1.2248578 -1.2248578 -1.2248578 1.0148821 -0.1049878
## [31] 1.0148821 -1.2248578
##
## [[3]]
## [1] -0.57061982 -0.57061982 -0.99018209 0.22009369 1.04308123 -0.04616698
## [7] 1.04308123 -0.67793094 -0.72553512 -0.50929918 -0.50929918 0.36371309
## [13] 0.36371309 0.36371309 1.94675381 1.84993175 1.68856165 -1.22658929
## [19] -1.25079481 -1.28790993 -0.89255318 0.70420401 0.59124494 0.96239618
## [25] 1.36582144 -1.22416874 -0.89093948 -1.09426581 0.97046468 -0.69164740
## [31] 0.56703942 -0.88529152
##
## [[4]]
## [1] -0.53509284 -0.53509284 -0.78304046 -0.53509284 0.41294217 -0.60801861
## [7] 1.43390296 -1.23518023 -0.75387015 -0.34548584 -0.34548584 0.48586794
## [13] 0.48586794 0.48586794 0.85049680 0.99634834 1.21512565 -1.17683962
## [19] -1.38103178 -1.19142477 -0.72469984 0.04831332 0.04831332 1.43390296
## [25] 0.41294217 -1.17683962 -0.81221077 -0.49133738 1.71102089 0.41294217
## [31] 2.74656682 -0.54967799
##
## [[5]]
## [1] 0.56751369 0.56751369 0.47399959 -0.96611753 -0.83519779 -1.56460776
## [7] -0.72298087 0.17475447 0.60491932 0.60491932 0.60491932 -0.98482035
## [13] -0.98482035 -0.98482035 -1.24665983 -1.11574009 -0.68557523 0.90416444
## [19] 2.49390411 1.16600392 0.19345729 -1.56460776 -0.83519779 0.24956575
## [25] -0.96611753 0.90416444 1.55876313 0.32437703 1.16600392 0.04383473
## [31] -0.10578782 0.96027290
##
## [[6]]
## [1] -0.610399567 -0.349785269 -0.917004624 -0.002299538 0.227654255
## [6] 0.248094592 0.360516446 -0.027849959 -0.068730634 0.227654255
## [11] 0.227654255 0.871524874 0.524039143 0.575139986 2.077504765
## [16] 2.255335698 2.174596366 -1.039646647 -1.637526508 -1.412682800
## [21] -0.768812180 0.309415603 0.222544170 0.636460997 0.641571082
## [26] -1.310481114 -1.100967659 -1.741772228 -0.048290296 -0.457097039
## [31] 0.360516446 -0.446876870
##
## [[7]]
## [1] -0.77716515 -0.46378082 0.42600682 0.89048716 -0.46378082 1.32698675
## [7] -1.12412636 1.20387148 2.82675459 0.25252621 0.58829513 -0.25112717
## [13] -0.13920420 0.08464175 0.07344945 -0.01608893 -0.23993487 0.90727560
## [19] 0.37564148 1.14790999 1.20946763 -0.54772305 -0.30708866 -1.36476075
## [25] -0.44699237 0.58829513 -0.64285758 -0.53093460 -1.87401028 -1.31439542
## [31] -1.81804880 0.42041067
##
## [[8]]
## [1] -0.8680278 -0.8680278 1.1160357 1.1160357 -0.8680278 1.1160357
## [7] -0.8680278 1.1160357 1.1160357 1.1160357 1.1160357 -0.8680278
## [13] -0.8680278 -0.8680278 -0.8680278 -0.8680278 -0.8680278 1.1160357
## [19] 1.1160357 1.1160357 1.1160357 -0.8680278 -0.8680278 -0.8680278
## [25] -0.8680278 1.1160357 -0.8680278 1.1160357 -0.8680278 -0.8680278
## [31] -0.8680278 1.1160357
##
## [[9]]
## [1] 1.1899014 1.1899014 1.1899014 -0.8141431 -0.8141431 -0.8141431
## [7] -0.8141431 -0.8141431 -0.8141431 -0.8141431 -0.8141431 -0.8141431
## [13] -0.8141431 -0.8141431 -0.8141431 -0.8141431 -0.8141431 1.1899014
## [19] 1.1899014 1.1899014 -0.8141431 -0.8141431 -0.8141431 -0.8141431
## [25] -0.8141431 1.1899014 1.1899014 1.1899014 1.1899014 1.1899014
## [31] 1.1899014 1.1899014
##
## [[10]]
## [1] 0.4235542 0.4235542 0.4235542 -0.9318192 -0.9318192 -0.9318192
## [7] -0.9318192 0.4235542 0.4235542 0.4235542 0.4235542 -0.9318192
## [13] -0.9318192 -0.9318192 -0.9318192 -0.9318192 -0.9318192 0.4235542
## [19] 0.4235542 0.4235542 -0.9318192 -0.9318192 -0.9318192 -0.9318192
## [25] -0.9318192 0.4235542 1.7789276 1.7789276 1.7789276 1.7789276
## [31] 1.7789276 0.4235542
##
## [[11]]
## [1] 0.7352031 0.7352031 -1.1221521 -1.1221521 -0.5030337 -1.1221521
## [7] 0.7352031 -0.5030337 -0.5030337 0.7352031 0.7352031 0.1160847
## [13] 0.1160847 0.1160847 0.7352031 0.7352031 0.7352031 -1.1221521
## [19] -0.5030337 -1.1221521 -1.1221521 -0.5030337 -0.5030337 0.7352031
## [25] -0.5030337 -1.1221521 -0.5030337 -0.5030337 0.7352031 1.9734398
## [31] 3.2116766 -0.5030337
lapply(mtcars, my_zscore)
## $mpg
## [1] 0.15088482 0.15088482 0.44954345 0.21725341 -0.23073453 -0.33028740
## [7] -0.96078893 0.71501778 0.44954345 -0.14777380 -0.38006384 -0.61235388
## [13] -0.46302456 -0.81145962 -1.60788262 -1.60788262 -0.89442035 2.04238943
## [19] 1.71054652 2.29127162 0.23384555 -0.76168319 -0.81145962 -1.12671039
## [25] -0.14777380 1.19619000 0.98049211 1.71054652 -0.71190675 -0.06481307
## [31] -0.84464392 0.21725341
##
## $cyl
## [1] -0.1049878 -0.1049878 -1.2248578 -0.1049878 1.0148821 -0.1049878
## [7] 1.0148821 -1.2248578 -1.2248578 -0.1049878 -0.1049878 1.0148821
## [13] 1.0148821 1.0148821 1.0148821 1.0148821 1.0148821 -1.2248578
## [19] -1.2248578 -1.2248578 -1.2248578 1.0148821 1.0148821 1.0148821
## [25] 1.0148821 -1.2248578 -1.2248578 -1.2248578 1.0148821 -0.1049878
## [31] 1.0148821 -1.2248578
##
## $disp
## [1] -0.57061982 -0.57061982 -0.99018209 0.22009369 1.04308123 -0.04616698
## [7] 1.04308123 -0.67793094 -0.72553512 -0.50929918 -0.50929918 0.36371309
## [13] 0.36371309 0.36371309 1.94675381 1.84993175 1.68856165 -1.22658929
## [19] -1.25079481 -1.28790993 -0.89255318 0.70420401 0.59124494 0.96239618
## [25] 1.36582144 -1.22416874 -0.89093948 -1.09426581 0.97046468 -0.69164740
## [31] 0.56703942 -0.88529152
##
## $hp
## [1] -0.53509284 -0.53509284 -0.78304046 -0.53509284 0.41294217 -0.60801861
## [7] 1.43390296 -1.23518023 -0.75387015 -0.34548584 -0.34548584 0.48586794
## [13] 0.48586794 0.48586794 0.85049680 0.99634834 1.21512565 -1.17683962
## [19] -1.38103178 -1.19142477 -0.72469984 0.04831332 0.04831332 1.43390296
## [25] 0.41294217 -1.17683962 -0.81221077 -0.49133738 1.71102089 0.41294217
## [31] 2.74656682 -0.54967799
##
## $drat
## [1] 0.56751369 0.56751369 0.47399959 -0.96611753 -0.83519779 -1.56460776
## [7] -0.72298087 0.17475447 0.60491932 0.60491932 0.60491932 -0.98482035
## [13] -0.98482035 -0.98482035 -1.24665983 -1.11574009 -0.68557523 0.90416444
## [19] 2.49390411 1.16600392 0.19345729 -1.56460776 -0.83519779 0.24956575
## [25] -0.96611753 0.90416444 1.55876313 0.32437703 1.16600392 0.04383473
## [31] -0.10578782 0.96027290
##
## $wt
## [1] -0.610399567 -0.349785269 -0.917004624 -0.002299538 0.227654255
## [6] 0.248094592 0.360516446 -0.027849959 -0.068730634 0.227654255
## [11] 0.227654255 0.871524874 0.524039143 0.575139986 2.077504765
## [16] 2.255335698 2.174596366 -1.039646647 -1.637526508 -1.412682800
## [21] -0.768812180 0.309415603 0.222544170 0.636460997 0.641571082
## [26] -1.310481114 -1.100967659 -1.741772228 -0.048290296 -0.457097039
## [31] 0.360516446 -0.446876870
##
## $qsec
## [1] -0.77716515 -0.46378082 0.42600682 0.89048716 -0.46378082 1.32698675
## [7] -1.12412636 1.20387148 2.82675459 0.25252621 0.58829513 -0.25112717
## [13] -0.13920420 0.08464175 0.07344945 -0.01608893 -0.23993487 0.90727560
## [19] 0.37564148 1.14790999 1.20946763 -0.54772305 -0.30708866 -1.36476075
## [25] -0.44699237 0.58829513 -0.64285758 -0.53093460 -1.87401028 -1.31439542
## [31] -1.81804880 0.42041067
##
## $vs
## [1] -0.8680278 -0.8680278 1.1160357 1.1160357 -0.8680278 1.1160357
## [7] -0.8680278 1.1160357 1.1160357 1.1160357 1.1160357 -0.8680278
## [13] -0.8680278 -0.8680278 -0.8680278 -0.8680278 -0.8680278 1.1160357
## [19] 1.1160357 1.1160357 1.1160357 -0.8680278 -0.8680278 -0.8680278
## [25] -0.8680278 1.1160357 -0.8680278 1.1160357 -0.8680278 -0.8680278
## [31] -0.8680278 1.1160357
##
## $am
## [1] 1.1899014 1.1899014 1.1899014 -0.8141431 -0.8141431 -0.8141431
## [7] -0.8141431 -0.8141431 -0.8141431 -0.8141431 -0.8141431 -0.8141431
## [13] -0.8141431 -0.8141431 -0.8141431 -0.8141431 -0.8141431 1.1899014
## [19] 1.1899014 1.1899014 -0.8141431 -0.8141431 -0.8141431 -0.8141431
## [25] -0.8141431 1.1899014 1.1899014 1.1899014 1.1899014 1.1899014
## [31] 1.1899014 1.1899014
##
## $gear
## [1] 0.4235542 0.4235542 0.4235542 -0.9318192 -0.9318192 -0.9318192
## [7] -0.9318192 0.4235542 0.4235542 0.4235542 0.4235542 -0.9318192
## [13] -0.9318192 -0.9318192 -0.9318192 -0.9318192 -0.9318192 0.4235542
## [19] 0.4235542 0.4235542 -0.9318192 -0.9318192 -0.9318192 -0.9318192
## [25] -0.9318192 0.4235542 1.7789276 1.7789276 1.7789276 1.7789276
## [31] 1.7789276 0.4235542
##
## $carb
## [1] 0.7352031 0.7352031 -1.1221521 -1.1221521 -0.5030337 -1.1221521
## [7] 0.7352031 -0.5030337 -0.5030337 0.7352031 0.7352031 0.1160847
## [13] 0.1160847 0.1160847 0.7352031 0.7352031 0.7352031 -1.1221521
## [19] -0.5030337 -1.1221521 -1.1221521 -0.5030337 -0.5030337 0.7352031
## [25] -0.5030337 -1.1221521 -0.5030337 -0.5030337 0.7352031 1.9734398
## [31] 3.2116766 -0.5030337
sapply(mtcars, my_zscore)
## mpg cyl disp hp drat wt
## [1,] 0.15088482 -0.1049878 -0.57061982 -0.53509284 0.56751369 -0.610399567
## [2,] 0.15088482 -0.1049878 -0.57061982 -0.53509284 0.56751369 -0.349785269
## [3,] 0.44954345 -1.2248578 -0.99018209 -0.78304046 0.47399959 -0.917004624
## [4,] 0.21725341 -0.1049878 0.22009369 -0.53509284 -0.96611753 -0.002299538
## [5,] -0.23073453 1.0148821 1.04308123 0.41294217 -0.83519779 0.227654255
## [6,] -0.33028740 -0.1049878 -0.04616698 -0.60801861 -1.56460776 0.248094592
## [7,] -0.96078893 1.0148821 1.04308123 1.43390296 -0.72298087 0.360516446
## [8,] 0.71501778 -1.2248578 -0.67793094 -1.23518023 0.17475447 -0.027849959
## [9,] 0.44954345 -1.2248578 -0.72553512 -0.75387015 0.60491932 -0.068730634
## [10,] -0.14777380 -0.1049878 -0.50929918 -0.34548584 0.60491932 0.227654255
## [11,] -0.38006384 -0.1049878 -0.50929918 -0.34548584 0.60491932 0.227654255
## [12,] -0.61235388 1.0148821 0.36371309 0.48586794 -0.98482035 0.871524874
## [13,] -0.46302456 1.0148821 0.36371309 0.48586794 -0.98482035 0.524039143
## [14,] -0.81145962 1.0148821 0.36371309 0.48586794 -0.98482035 0.575139986
## [15,] -1.60788262 1.0148821 1.94675381 0.85049680 -1.24665983 2.077504765
## [16,] -1.60788262 1.0148821 1.84993175 0.99634834 -1.11574009 2.255335698
## [17,] -0.89442035 1.0148821 1.68856165 1.21512565 -0.68557523 2.174596366
## [18,] 2.04238943 -1.2248578 -1.22658929 -1.17683962 0.90416444 -1.039646647
## [19,] 1.71054652 -1.2248578 -1.25079481 -1.38103178 2.49390411 -1.637526508
## [20,] 2.29127162 -1.2248578 -1.28790993 -1.19142477 1.16600392 -1.412682800
## [21,] 0.23384555 -1.2248578 -0.89255318 -0.72469984 0.19345729 -0.768812180
## [22,] -0.76168319 1.0148821 0.70420401 0.04831332 -1.56460776 0.309415603
## [23,] -0.81145962 1.0148821 0.59124494 0.04831332 -0.83519779 0.222544170
## [24,] -1.12671039 1.0148821 0.96239618 1.43390296 0.24956575 0.636460997
## [25,] -0.14777380 1.0148821 1.36582144 0.41294217 -0.96611753 0.641571082
## [26,] 1.19619000 -1.2248578 -1.22416874 -1.17683962 0.90416444 -1.310481114
## [27,] 0.98049211 -1.2248578 -0.89093948 -0.81221077 1.55876313 -1.100967659
## [28,] 1.71054652 -1.2248578 -1.09426581 -0.49133738 0.32437703 -1.741772228
## [29,] -0.71190675 1.0148821 0.97046468 1.71102089 1.16600392 -0.048290296
## [30,] -0.06481307 -0.1049878 -0.69164740 0.41294217 0.04383473 -0.457097039
## [31,] -0.84464392 1.0148821 0.56703942 2.74656682 -0.10578782 0.360516446
## [32,] 0.21725341 -1.2248578 -0.88529152 -0.54967799 0.96027290 -0.446876870
## qsec vs am gear carb
## [1,] -0.77716515 -0.8680278 1.1899014 0.4235542 0.7352031
## [2,] -0.46378082 -0.8680278 1.1899014 0.4235542 0.7352031
## [3,] 0.42600682 1.1160357 1.1899014 0.4235542 -1.1221521
## [4,] 0.89048716 1.1160357 -0.8141431 -0.9318192 -1.1221521
## [5,] -0.46378082 -0.8680278 -0.8141431 -0.9318192 -0.5030337
## [6,] 1.32698675 1.1160357 -0.8141431 -0.9318192 -1.1221521
## [7,] -1.12412636 -0.8680278 -0.8141431 -0.9318192 0.7352031
## [8,] 1.20387148 1.1160357 -0.8141431 0.4235542 -0.5030337
## [9,] 2.82675459 1.1160357 -0.8141431 0.4235542 -0.5030337
## [10,] 0.25252621 1.1160357 -0.8141431 0.4235542 0.7352031
## [11,] 0.58829513 1.1160357 -0.8141431 0.4235542 0.7352031
## [12,] -0.25112717 -0.8680278 -0.8141431 -0.9318192 0.1160847
## [13,] -0.13920420 -0.8680278 -0.8141431 -0.9318192 0.1160847
## [14,] 0.08464175 -0.8680278 -0.8141431 -0.9318192 0.1160847
## [15,] 0.07344945 -0.8680278 -0.8141431 -0.9318192 0.7352031
## [16,] -0.01608893 -0.8680278 -0.8141431 -0.9318192 0.7352031
## [17,] -0.23993487 -0.8680278 -0.8141431 -0.9318192 0.7352031
## [18,] 0.90727560 1.1160357 1.1899014 0.4235542 -1.1221521
## [19,] 0.37564148 1.1160357 1.1899014 0.4235542 -0.5030337
## [20,] 1.14790999 1.1160357 1.1899014 0.4235542 -1.1221521
## [21,] 1.20946763 1.1160357 -0.8141431 -0.9318192 -1.1221521
## [22,] -0.54772305 -0.8680278 -0.8141431 -0.9318192 -0.5030337
## [23,] -0.30708866 -0.8680278 -0.8141431 -0.9318192 -0.5030337
## [24,] -1.36476075 -0.8680278 -0.8141431 -0.9318192 0.7352031
## [25,] -0.44699237 -0.8680278 -0.8141431 -0.9318192 -0.5030337
## [26,] 0.58829513 1.1160357 1.1899014 0.4235542 -1.1221521
## [27,] -0.64285758 -0.8680278 1.1899014 1.7789276 -0.5030337
## [28,] -0.53093460 1.1160357 1.1899014 1.7789276 -0.5030337
## [29,] -1.87401028 -0.8680278 1.1899014 1.7789276 0.7352031
## [30,] -1.31439542 -0.8680278 1.1899014 1.7789276 1.9734398
## [31,] -1.81804880 -0.8680278 1.1899014 1.7789276 3.2116766
## [32,] 0.42041067 1.1160357 1.1899014 0.4235542 -0.5030337
i <- 1
while (i<=3) {
print(i^2)
i=i+1
}
## [1] 1
## [1] 4
## [1] 9
score <- 93
grade <- "Fail"
if (score > 90) {
grade <- "Success"
}
grade
## [1] "Success"
score <- 85
grade <- "Fail"
if (score > 90) {
grade <- "Success"
}
grade
## [1] "Fail"
score <-85
if (score > 90) {
grade <- "Success"
} else {
grade <- "Fail"
}
grade
## [1] "Fail"
score <- 75
if (score > 90) {
grade <- "A"
} else if (score > 80) {
grade <- "B"
} else {
grade <- "C"
}
grade
## [1] "C"
x <- c(2,5,3,9,8,11,6)
count <- 0
for (value in x) {
if(value %% 2 == 0)
count = count +1
}
count
## [1] 3
grade <- "A"
if (grade == "A") {
grade <- "수"
} else if (grade == "B") {
grade <- "우"
} else if (grade =="C") {
grade <- "미"
} else if (grade =="D") {
grade <- "양"
} else if (grade =="F") {
grade <- "가"
}
grade
## [1] "수"
grade <- "A"
grade <- switch(grade,
"A"="수",
"B"="우",
"C"="미",
"D"="양",
"F"="가"
)
grade
## [1] "수"
grades <- c("A", "C", "D", "B", "A", "F")
new_grades <- vector("character", length(grade))
for (i in 1:length(grades)) {
new_grades[i] <- switch(grades[i],
"A"="수",
"B"="우",
"C"="미",
"D"="양",
"F"="가"
)
}
new_grades
## [1] "수" "미" "양" "우" "수" "가"
dplyr::recode(grades,
"A"="수",
"B"="우",
"C"="미",
"D"="양",
"F"="가"
)
## [1] "수" "미" "양" "우" "수" "가"
score <-85
if (score > 90) {
grade <- "Success"
} else {
grade <- "Fail"
}
grade
## [1] "Fail"
scores <- c(85, 93, 72, 98, 95)
new_scores <- vector("double", length(scores))
for (i in 1:length(scores)) {
if(scores[i]>90) {
new_scores[i] <- "Success"
} else {
new_scores[i] <- "Fail"
}
}
new_scores
## [1] "Fail" "Success" "Fail" "Success" "Success"
scores <- c(85, 93, 72, 98, 95)
dplyr::if_else(scores>90, "Success", "Fail")
## [1] "Fail" "Success" "Fail" "Success" "Success"