[R] Merge Start

BioinformaticsAndMe





R Merge?


: R에서는 두 개의 data.frame을 병합하기 위해, Merge 함수를 사용

*데이터프레임들은 최소 1개 이상의 동일한 column(열)을 가져야 함

: merge(x, y, by, all)

* x - 데이터프레임1

* y - 데이터프레임2

* by - 공통된 column (동일 변수)

* all - Merging의 형태 (default값=FALSE)

# 데이터 준비 (첫번째 data.frame) df1 = data.frame(CustomerId = c(1:6), Product = c(rep("Oven", 3), rep("Television", 3))) df1

CustomerId Product 1 1 Oven 2 2 Oven 3 3 Oven 4 4 Television 5 5 Television 6 6 Television

# 데이터 준비 (두번째 data.frame) df2 = data.frame(CustomerId = c(2, 4, 6), State = c(rep("California", 2), rep("Texas", 1))) df2

CustomerId State 1 2 California 2 4 California 3 6 Texas





Merge 형태


1) Natural join: 두 데이터프레임에서 공통된 행만 유지 (all=FALSE)

2) Full outer join: 두 데이터프레임에서 모든 행을 유지 (all=TRUE)

3) Left outer join: 두 데이터프레임 중 좌측 x 프레임의 모든 행을 유지 (x=TRUE)

4) Right outer join: 두 데이터프레임 중 우측 y 프레임의 모든 행을 유지 (y=TRUE)


1) Natural join (Inner join) df<-merge(x=df1,y=df2,by="CustomerId") df

CustomerId Product State 1 2 Oven California 2 4 Television California 3 6 Television Texas

2) Full outer join df<-merge(x=df1,y=df2,by="CustomerId",all=TRUE) df

CustomerId Product State 1 1 Oven <NA> 2 2 Oven California 3 3 Oven <NA> 4 4 Television California 5 5 Television <NA> 6 6 Television Texas

3) Left outer join df<-merge(x=df1,y=df2,by="CustomerId",all.x=TRUE) df

CustomerId Product State 1 1 Oven <NA> 2 2 Oven California 3 3 Oven <NA> 4 4 Television California 5 5 Television <NA> 6 6 Television Texas

4) Right outer join

df<-merge(x=df1,y=df2,by="CustomerId",all.y=TRUE) df

CustomerId Product State 1 2 Oven California 2 4 Television California 3 6 Television Texas




#Reference

1) http://www.datasciencemadesimple.com/join-in-r-merge-in-r/





[R] Merge End

BioinformaticsAndMe


'R' 카테고리의 다른 글

[R] 히스토그램(Histogram)  (0) 2019.11.18
[R] Excel 읽기/쓰기  (0) 2019.11.05
[R] Heatmap 시각화  (4) 2019.10.24
[R] transform (데이터열생성 함수)  (0) 2019.10.17
[R] subset (데이터추출 함수)  (0) 2019.10.17

+ Recent posts