빅데이터/R을 이용한 빅데이터분석

R 데이터 형태 ( vector, matrix, array, list, dataframe)

YJ_SW 2018. 8. 31. 12:51
728x90

R의 데이터 형태


① vector : 여러 개의 동일한 형태의 데이터를 모아서 함께 저장 (동일한 데이터형)

> vec <- c(1,2,3,4,5)

> vec

[1] 1 2 3 4 5

> vec[3]      # 3번째 요소

[1] 3

> vec[-3]     # 3번째 요소만 빼고 출력

[1] 1 2 4 5

> vec[2:4]     # 2부터 4번째 까지 출력

[1] 2 3 4

> vec[2]<-6

> vec

[1] 1 6 3 4 5

> append(vec,10,after=3)     # 벡터에 새로운 요소 추가

[1]  1  6  3 10  4  5

> append(vec,11,after=3)

[1]  1  6  3 11  4  5

> append(vec,12,after=0)

[1] 12  1  6  3  4  5

> vec

[1] 1 6 3 4 5

> vec2<-append(vec,13,after=3)

> vec2

[1]  1  6  3 13  4  5



> fruits <- c(10,20,30)

> fruits

[1] 10 20 30

> names(fruits) <- c('apple','banana','peach')     # 각 컬럼에 이름 지정

> fruits

 apple banana  peach 

    10     20     30 




> var2 <- seq(1,5) ; var2

[1] 1 2 3 4 5

> var3 <- seq(1,10,2) ; var3

[1] 1 3 5 7 9


② matrix(행렬) : 여러 행과 여러 컬럼으로 이루어진 데이터 저장 (동일한 데이터형)

     cf ) 벡터는 한 행만 사용가능

> mat1 <- matrix(c(1,2,3,4))

> mat1

     [,1]

[1,]    1

[2,]    2

[3,]    3

[4,]    4

> mat2 <- matrix(c(1,2,3,4),nrow=2)     # nrow를 사용해 2행으로 만들기

> mat2

     [,1] [,2]

[1,]    1    3

[2,]    2    4

> mat3 <- matrix(c(1,2,3,4),nrow=2,byrow=T)     # 가로로 입력

> mat3

     [,1] [,2]

[1,]    1    2

[2,]    3    4


> mat4 <- matrix(c(1,2,3,

+                  4,5,6,

+                  7,8,9),nrow=3,byrow=T)

> mat4

     [,1] [,2] [,3]

[1,]    1    2    3

[2,]    4    5    6

[3,]    7    8    9

> mat4 <- rbind(mat4,c(11,12,13))     # 행추가

> mat4

     [,1] [,2] [,3]

[1,]    1    2    3

[2,]    4    5    6

[3,]    7    8    9

[4,]   11   12   13

> mat4 <- cbind(mat4, c(100,200,300))     # 길이가 다를 경우 에러 발생

Warning message:

In cbind(mat4, c(100, 200, 300)) :

  number of rows of result is not a multiple of vector length (arg 2)

> mat4 <- cbind(mat4, c(100,200,300,400))     # 열 추가

> mat4

     [,1] [,2] [,3] [,4] [,5]

[1,]    1    2    3  100  100

[2,]    4    5    6  200  200

[3,]    7    8    9  300  300

[4,]   11   12   13  100  400

> mat4 <-mat4[,-4]     # 열 삭제 

> mat4

     [,1] [,2] [,3] [,4]

[1,]    1    2    3  100

[2,]    4    5    6  200

[3,]    7    8    9  300

[4,]   11   12   13  400


array(배열) : 3차원 데이터 저장 ( 가로, 세로, 높이)

> array1 <- array(c(1:12),dim=c(4,3))

> array1

     [,1] [,2] [,3]

[1,]    1    5    9

[2,]    2    6   10

[3,]    3    7   11

[4,]    4    8   12

> array2 <- array(c(1:12),dim=c(2,2,3))   # 삼차원 배열

> array2

, , 1     # 1층


     [,1] [,2]

[1,]    1    3

[2,]    2    4


, , 2     # 2층


     [,1] [,2]

[1,]    5    7

[2,]    6    8


, , 3


     [,1] [,2]

[1,]    9   11

[2,]   10   12


> array2[1,1,3]    # x:1, y:1, z:3 인 데이터 조회

[1] 9



vector, matrix, array 는 동일한 데이터형만 저장 가능


④ list : 다른 유형의 데이터 저장

> list1 <- list(name = 'Yeonjae',

+               address='Seoul',

+               tel='010-1234-1234',

+               pay=1000)

> list1

$name    #key

[1] "Yeonjae"    #value


$address

[1] "Seoul"


$tel

[1] "010-1234-1234"


$pay

[1] 1000


> list1$name

[1] "Yeonjae"

> list1[1:2]

$name

[1] "Yeonjae"


$address

[1] "Seoul"


> list1$birth <- '1996-04-07'     # 새로운 요소 추가

> list1

$name

[1] "Yeonjae"


$address

[1] "Seoul"


$tel

[1] "010-1234-1234"


$pay

[1] 1000


$birth

[1] "1996-04-07"


⑤ dataframe : 다른 데이터형을 가진 2차원의 데이터 구조, 각각의 행들이 서로다른 데이터 타입

> # 벡터로 부터 데이터 프레임 생성하기 

> no <- c(1,2,3,4)

> name <- c ('Apple','Peach','Banana','Grape')

> price <- c(500,200,100,50)

> qty <- c(5,2,4,7)

> sales <-data.frame(NO=no,NAME=name,PRICE=price,QTY=qty)

> sales

  NO   NAME PRICE QTY

1  1  Apple   500   5

2  2  Peach   200   2

3  3 Banana   100   4

4  4  Grape    50   7

> # 행렬로 부터 데이터 프레임 생성하기 

> sales2 <- matrix(c(1,'Apple',500,5,

+                    2,'Peach',200,2)

+                  ,nrow=2,byrow=T)

> df1 <- data.frame(sales2)

> df1

  X1    X2  X3 X4

1  1 Apple 500  5

2  2 Peach 200  2

> names(df1) <- c('NO','NAME','PRICE','QTY')    # 라벨명 저장

> df1

  NO  NAME PRICE QTY

1  1 Apple   500   5

2  2 Peach   200   2

> sales$NAME

[1] Apple  Peach  Banana Grape 

Levels: Apple Banana Grape Peach

> subset(sales,price==200)

  NO  NAME PRICE QTY

2  2 Peach   200   2

> df1<-data.frame(name=c('apple','banana','cherry'),price=c(300,200,100))

> df2<-data.frame(name=c('apple','cherry','berry'),qty=c(10,20,30))

> df1

    name price

1  apple   300

2 banana   200

3 cherry   100

> df2

    name qty

1  apple  10

2 cherry  20

3  berry  30

> merge(df1,df2)     #공통으로 있는 name 컬럼 데이터 출력

    name price qty

1  apple   300  10

2 cherry   100  20

> merge(df1,df2,all=T)    #데이터가 없는 것도 모두 나옴

    name price qty

1  apple   300  10

2 banana   200  NA

3 cherry   100  20

4  berry    NA  30

> cbind(df1,df2)     #df1과 df2를 합쳐 하나의 데이터 프레임으로 만듦

    name price   name qty

1  apple   300  apple  10

2 banana   200 cherry  20

3 cherry   100  berry  30

> rbind(df1,df2)






서진수.R라뷰」더알음.

728x90