🤖 Education/2019AI개발자양성과정(과기부)

[R_STUDY] 04. Workshop

데이터분석가SIENNA 2023. 1. 30. 02:13

1) 숫자 표현

#5 + 6의2승
> 5 + 6^2
[1] 41

> #log10 10
> log10(10)
[1] 1

> #logee
> log(exp(exp(1)))
[1] 2.718282

> #e의 2승
> exp(2)
[1] 7.389056

 

2) 파일 읽어오기

#workshop2.txt 읽어오기
> path <- "C:\\Users\\user\\Desktop\\data\\workshop2.txt"
> f <- read.table(path, header=T, encoding="UTF-8", stringsAsFactors = F)
> f
   name weight height
1   Lee     93   1.72
2   Kim     82   1.81
3 Kueon     80   1.70
4  Kang     67   1.69
5  Choi     72   1.83
6   Keo     69   1.76

 

3) 테이블 정보로 BMI 구하기

 #BMI구하기 (weight / height^2)
> y
   name weight height
1   Lee     93   1.72
2   Kim     82   1.81
3 Kueon     80   1.70
4  Kang     67   1.69
5  Choi     72   1.83
6   Keo     69   1.76
7   Han     62   1.73

> y1 <- y[,-1]
> y1
  weight height
1     93   1.72
2     82   1.81
3     80   1.70
4     67   1.69
5     72   1.83
6     69   1.76
7     62   1.73

> y2 <- y1[,-2]
> y2
[1] 93 82 80 67 72 69 62

> y3 <- y1[,-1]
> y3
[1] 1.72 1.81 1.70 1.69 1.83 1.76 1.73

> y4 <-(y3*y3)
> y4
[1] 2.9584 3.2761 2.8900 2.8561 3.3489 3.0976 2.9929

> BMI <- y2/y4
> BMI
[1] 31.43591 25.02976 27.68166 23.45856 21.49960 22.27531 20.71569

(* 잘 모르겠어서 y에서 열을 잘라와 y2, y3에 넣음ㅋㅋㅋㅋㅋ)

 

 

4) BMI 컬럼 추가하기

#BMI 컬럼추가하기
> y <- cbind(y, BMI)
> y
   name weight height      BMI
1   Lee     93   1.72 31.43591
2   Kim     82   1.81 25.02976
3 Kueon     80   1.70 27.68166
4  Kang     67   1.69 23.45856
5  Choi     72   1.83 21.49960
6   Keo     69   1.76 22.27531
7   Han     62   1.73 20.71569

 

5) 몸무게의 평균, 표준편차 구하기

- 평균 : mean(x)

- 표준편차 : sd(x)

#평균
> mean(y2)
[1] 75

#표준편차
> sd(y2)
[1] 10.61446

 

6) BMI 카테고리 만들어 컬럼 추가하기

Underweight =< 18.5

Normal weight = 18.5-24.9

Overweight =25-29.9

Obesity = BMI of 30 or greater

#BMI CATEGORIES
> x <- c()
> x
NULL

> for(s in BMI){
+ if(s <= 18.5) {
+ x <-c(x, "Underweight")
+ }   else if (s <=24.9){
+ x <-c(x, "Normal weight")
+ } else if (s <=29.9){
+ x <-c(x, "Overweight")
+ }   else {
+ x <-c(x, "Obesity")
+ }
+ }

> x
[1] "Obesity"       "Overweight"    "Overweight"    "Normal weight" "Normal weight"
[6] "Normal weight" "Normal weight"
#BMI CATEGORY 컬럼 추가하기
> y <- cbind(y, COMMENT = x)
> y
   name weight height      BMI       COMMENT
1   Lee     93   1.72 31.43591       Obesity
2   Kim     82   1.81 25.02976    Overweight
3 Kueon     80   1.70 27.68166    Overweight
4  Kang     67   1.69 23.45856 Normal weight
5  Choi     72   1.83 21.49960 Normal weight
6   Keo     69   1.76 22.27531 Normal weight
7   Han     62   1.73 20.71569 Normal weight

 

#선생님이 하신 방법

#파일읽어오기
path <- "C:\\Users\\user\\Desktop\\data\\workshop2.txt"
> workshop2 <- read.table(path, header=T, stringsAsFactors=F)
> str(workshop2)
'data.frame':   6 obs. of  3 variables:
 $ name  : chr  "Lee" "Kim" "Kueon" "Kang" ...
 $ weight: int  93 82 80 67 72 69
 $ height: num  1.72 1.81 1.7 1.69 1.83 1.76

> workshop2
   name weight height
1   Lee     93   1.72
2   Kim     82   1.81
3 Kueon     80   1.70
4  Kang     67   1.69
5  Choi     72   1.83
6   Keo     69   1.76

> workshop2 <- rbind(workshop2, list("Han", 62, 1.73))

> str(workshop2)
'data.frame':   7 obs. of  3 variables:
 $ name  : chr  "Lee" "Kim" "Kueon" "Kang" ...
 $ weight: num  93 82 80 67 72 69 62
 $ height: num  1.72 1.81 1.7 1.69 1.83 1.76 1.73

#BMI 구하기
BMI <- workshop2$weight / workshop2$height^2
> BMI
[1] 31.43591 25.02976 27.68166 23.45856 21.49960 22.27531 20.71569

> workshop2 <- cbind(workshop2, BMI)

> str(workshop2)
'data.frame':   7 obs. of  4 variables:
 $ name  : chr  "Lee" "Kim" "Kueon" "Kang" ...
 $ weight: num  93 82 80 67 72 69 62
 $ height: num  1.72 1.81 1.7 1.69 1.83 1.76 1.73
 $ BMI   : num  31.4 25 27.7 23.5 21.5 ...
 
> workshop2
   name weight height      BMI
1   Lee     93   1.72 31.43591
2   Kim     82   1.81 25.02976
3 Kueon     80   1.70 27.68166
4  Kang     67   1.69 23.45856
5  Choi     72   1.83 21.49960
6   Keo     69   1.76 22.27531
7   Han     62   1.73 20.71569

#BMI카테고리 생성
> comments <- workshop2$BMI

> comments
[1] 31.43591 25.02976 27.68166 23.45856 21.49960 22.27531 20.71569
> comments[workshop2$BMI < 18.5] <- "Under"
> comments[workshop2$BMI >=18.5 & workshop2$BMI < 25] <- "Normal"
> comments[workshop2$BMI >=25 & workshop2$BMI < 30] <- "Over"
> comments[workshop2$BMI >=30] <- "Obesity"

> comments                     
[1] "Obesity" "Over"    "Over"    "Normal"  "Normal"  "Normal"  "Normal" 
workshop2 <- cbind(workshop2, comments)

> str(workshop2)
'data.frame':   7 obs. of  5 variables:
 $ name    : chr  "Lee" "Kim" "Kueon" "Kang" ...
 $ weight  : num  93 82 80 67 72 69 62
 $ height  : num  1.72 1.81 1.7 1.69 1.83 1.76 1.73
 $ BMI     : num  31.4 25 27.7 23.5 21.5 ...
 $ comments: Factor w/ 3 levels "Normal","Obesity",..: 2 3 3 1 1 1 1
 
> workshop2
   name weight height      BMI comments
1   Lee     93   1.72 31.43591  Obesity
2   Kim     82   1.81 25.02976     Over
3 Kueon     80   1.70 27.68166     Over
4  Kang     67   1.69 23.45856   Normal
5  Choi     72   1.83 21.49960   Normal
6   Keo     69   1.76 22.27531   Normal
7   Han     62   1.73 20.71569   Normal