본문 바로가기
Data Science/R

[R] 데이터 변수 추출 및 기본 함수

by AI_Wooah 2022. 3. 1.

https://polar-comet-18d.notion.site/R-a249d0c3ce9e47019c87887b4a2fd1fc

 

[R] 데이터 변수 추출 및 기본 함수

꽃받침의 길이 확인

polar-comet-18d.notion.site

 

꽃받침의 길이 확인

iris$Sepal.Length

💡
[1] "more then 5" "less than 5" "less than 5" "less than 5" "less than 5" "more then 5" "less than 5" [8] "less than 5" "less than 5" "less than 5" "more then 5" "less than 5" "less than 5" "less than 5" [15] "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [22] "more then 5" "less than 5" "more then 5" "less than 5" "less than 5" "less than 5" "more then 5" [29] "more then 5" "less than 5" "less than 5" "more then 5" "more then 5" "more then 5" "less than 5" [36] "less than 5" "more then 5" "less than 5" "less than 5" "more then 5" "less than 5" "less than 5" [43] "less than 5" "less than 5" "more then 5" "less than 5" "more then 5" "less than 5" "more then 5" [50] "less than 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [57] "more then 5" "less than 5" "more then 5" "more then 5" "less than 5" "more then 5" "more then 5" [64] "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [71] "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [78] "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [85] "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [92] "more then 5" "more then 5" "less than 5" "more then 5" "more then 5" "more then 5" "more then 5" [99] "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [106] "more then 5" "less than 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [113] "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [120] "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [127] "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [134] "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [141] "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" "more then 5" [148] "more then 5" "more then 5" "more then 5”

조건문 만들기

아이리스의 꽃받침의 길이가 5 이상인 것과 이하인 것에 각각 다른 구문을 출력하도록 조건문 생성

y<-ifelse(
iris$Sepal.Length>5, "more then 5", "less than 5"
)
y
#변수 생성 후 y 값 입력
iris$Sepal.Length_level<-y
head(iris)

head(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_level 1 5.1 3.5 1.4 0.2 setosa more then 5 2 4.9 3.0 1.4 0.2 setosa less than 5 3 4.7 3.2 1.3 0.2 setosa less than 5 4 4.6 3.1 1.5 0.2 setosa less than 5 5 5.0 3.6 1.4 0.2 setosa less than 5 6 5.4 3.9 1.7 0.4 setosa more then 5

데이터의 통합

aggregate 함수를 사용(변수~통합을 수행할 기준 변수, data=데이터명, ㅡmean)

Species Sepal.Length 1 setosa 5.006 2 versicolor 5.936 3 virginica 6.588

평균값 구하기

aggregate(Sepal.Length ~ Species, data=iris,mean)

합산값 구하기

aggregate(Sepal.Length ~ Species, data=iris,sum)

Species Sepal.Length 1 setosa 250.3 2 versicolor 296.8 3 virginica 329.4

4개의 변수에 대한 평균값 구하기 .은 모든 변수를 의미함

aggregate(. ~ Species, data=iris,mean)
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.006 3.428 1.462 0.246
2 5.936 2.770 4.260 1.326
3 6.588 2.974 5.552 2.026

 

반응형

댓글