7l log

[R데이터분석] 벡터 본문

2022

[R데이터분석] 벡터

HAPAGO 2022. 4. 24. 15:55

벡터

 

벡터는 데이터 구조의 가장 기본형태- 1차원이며 한가지 데이터 유형만

할당연산자( ←) 와 C()함수 combine 의 약자 를 이용함

변수명 ← c (값)

숫자형(numeric), 정수형(integer), 문자형(character), 논리형(logical) 중 한 가지 유형으로만

 

 

숫자형 벡터

 

숫자형 벡터 생성

ex_vector1 <- c(-1, 0, 1)

#조회

ex_vector1

[1] -1 0 1

[1] : 1차원 이라는 뜻

 

속성 확인하기

#mode()함수

mode(ex_vector1)

[1] "numeric”

#typeof()함수

typeof(ex_vector1)

[1] "double"

#str() 함수
str(ex_vector1)

num [1:3] -1 0 1

 

#length() 함수

length(ex_vector1)

[1] 3

 

mode(), typeof() 함수는 데이터형만 출력

mode() 함수는 모두 numeric으로 출력

typeof()함수는 정수(integer) 와 실수(double) 구분

str() 함수는 데이터형과 값을 같이 출력

 

문자형 벡터

반드시 “”로 묶어준다.

ex_vector2 <- c("Hello", "Hi~!")

ex_vector2

[1] "Hello" "Hi~!"

 

mode(ex_vector2)

[1] "character"

 

str(ex_vector2)

chr [1:2] "Hello" "Hi~!"

 

ex_vector3 <- c("1", "2", "3")

ex_vector3

[1] "1" "2" "3”

 

mode(ex_vector3)

[1] "character"

str(ex_vector3)

chr [1:3] "1" "2" "3"

 

#변수 삭제

remove()함수
remove(ex_vector2)

ex_vector2

#삭제됨 Error: object 'ex_vector2' not found

 

#ex_vector3삭제

rm()함수

rm(ex_vector3)

ex_vector3

#삭제됨 Error: object 'ex_vector3' not found

 

 

논리형벡터

r에서 논리형데이터는 all 대문자

ex_vector4 <- c(TRUE, FALSE, TRUE, FALSE)

#조회하기

ex_vector4 [1] TRUE FALSE TRUE FALSE

#속성확인하기

mode(ex_vector4)

[1] "logical"

 

str(ex_vector4)

logi [1:4] TRUE FALSE TRUE FALSE

 

범주형자료

범주형데이터는 카테코리 데이터 categorical data

특수화된 형태의 벡터로 이루어져 있다.

숫자가 없는 명목형 자료를 바탕으로 범주화한 데이터

범주형데이터 ↔ 수치형데이터

 

#factor() 함수 사용하여 범주형 데이터 생성

factor(범주화할 자료, labels = c(”범주1”, “범주2”))

범주화할 자료 ex_vector5 생성

ex_vector5 <- c(2, 1, 3, 2, 1)

ex_vector5 [1] 2 1 3 2 1

#cate_vector5라는 변수에 범주형 데이터 생성

cate_vector5 <- factor(ex_vector5, labels = c("Apple", "Banana", "Cherry"))

cate_vector5

[1] Banana Apple Cherry Banana Apple Levels: Apple Banana Cherry

#범주화한 자료의 순서에 맞추어 출력됨

Comments