DBILITY

R data divide, merge, extract ( 데이터 분할, 병합, 부분추출 ) 본문

statistics/R

R data divide, merge, extract ( 데이터 분할, 병합, 부분추출 )

DBILITY 2018. 11. 28. 10:47
반응형

split()함수는 데이터를 분할하는데 사용하며,

split(x,f) 형태로 x는 분리대상, f는 분할 조건으로 list로 반환한다.

 

subset()함수는 부분집합을 구하며, SQL의 SELECT와 유사하다.

subset(x , subset , select) 형태로, x는 subset을 추출할 대상,subset은 추출 조건, select는 대상열로 생각하면 되겠다.

 

merge()함수는 두 데이터 프레임을 공통된 컬럼값을 기준으로 연결하며,SQL의 JOIN과 유사하다.

merge(x, y, by = intersect(names(x), names(y)),

      by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all,

      sort = TRUE, suffixes = c(".x",".y"), no.dups = TRUE,

      incomparables = NULL, ...)

all옵션에 TRUE지정시 SQL의 FULL OUTER JOIN과 같이 동작하며 빈값은 NA처리된다.

> x<-split(iris,iris$Species)
> class(x)
[1] "list"
> lapply(split(iris$Sepal.Length,iris$Species),mean)
$`setosa`
[1] 5.006

$versicolor
[1] 5.936

$virginica
[1] 6.588
> subset(iris,Species=="setosa" & Sepal.Length > 5,select = c(Sepal.Length))
   Sepal.Length
1           5.1
6           5.4
11          5.4
15          5.8
16          5.7
17          5.4
18          5.1
19          5.7
20          5.1
21          5.4
22          5.1
24          5.1
28          5.2
29          5.2
32          5.4
33          5.2
34          5.5
37          5.5
40          5.1
45          5.1
47          5.1
49          5.3

> subset(iris,Species=="setosa" & Sepal.Length > 5,select = -c(Sepal.Length))
   Sepal.Width Petal.Length Petal.Width Species
1          3.5          1.4         0.2  setosa
6          3.9          1.7         0.4  setosa
11         3.7          1.5         0.2  setosa
15         4.0          1.2         0.2  setosa
16         4.4          1.5         0.4  setosa
17         3.9          1.3         0.4  setosa
18         3.5          1.4         0.3  setosa
19         3.8          1.7         0.3  setosa
20         3.8          1.5         0.3  setosa
21         3.4          1.7         0.2  setosa
22         3.7          1.5         0.4  setosa
24         3.3          1.7         0.5  setosa
28         3.5          1.5         0.2  setosa
29         3.4          1.4         0.2  setosa
32         3.4          1.5         0.4  setosa
33         4.1          1.5         0.1  setosa
34         4.2          1.4         0.2  setosa
37         3.5          1.3         0.2  setosa
40         3.4          1.5         0.2  setosa
45         3.8          1.9         0.4  setosa
47         3.8          1.6         0.2  setosa
49         3.7          1.5         0.2  setosa

> apply(subset(iris,Species=='setosa',select=c(Sepal.Length)),2,mean)
Sepal.Length 
       5.006 
> apply(subset(iris,Species=='setosa',select=-c(Species)),2,mean)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
       5.006        3.428        1.462        0.246 
> apply(subset(iris,Species=='setosa',select=-c(Species)),2,mean,na.rm=TRUE)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
       5.006        3.428        1.462        0.246 

> x<-data.frame(name=c('a','b','c'),math=c(1,2,3))
> y<-data.frame(name=c('c','b','a'),english=c(4,5,6))
> merge(x,y)
  name math english
1    a    1       6
2    b    2       5
3    c    3       4

> x<-data.frame(name=c('a','b','c'),math=c(1,2,3))
> y<-data.frame(name=c('d','b','a'),english=c(4,5,6))
> merge(x,y)
  name math english
1    a    1       6
2    b    2       5
> merge(x,y,all=TRUE)
  name math english
1    a    1       6
2    b    2       5
3    c    3      NA
4    d   NA       4
#응용해 보자
> library(doBy)
#iris 데이터 프레임에서 두개의 subset을 추출했다
> x<-subset(iris,select=c(Species,Sepal.Length))
> y<-subset(iris,select=c(Species,Sepal.Width))

#추출한 x,y 데이터 프레임에 row  index로 컬럼을 추가했다. 
#추가이유는 merge시 cartesian product가 발생하면 안되니까..
> x$index<-as.numeric(row.names(x))
> y$index<-as.numeric(row.names(y))
> str(x)
'data.frame':	150 obs. of  3 variables:
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ index       : num  1 2 3 4 5 6 7 8 9 10 ...
> str(y)
'data.frame':	150 obs. of  3 variables:
 $ Species    : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Sepal.Width: num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ index      : num  1 2 3 4 5 6 7 8 9 10 ...
#x,y 데이터 프레임을 병합
> z=merge(x,y,by=intersect(names(x), names(y)))
> str(z)
'data.frame':	150 obs. of  4 variables:
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ index       : num  1 10 11 12 13 14 15 16 17 18 ...
 $ Sepal.Length: num  5.1 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 ...
 $ Sepal.Width : num  3.5 3.1 3.7 3.4 3 3 4 4.4 3.9 3.5 ...
> head(z)
  Species index Sepal.Length Sepal.Width
1  setosa     1          5.1         3.5
2  setosa    10          4.9         3.1
3  setosa    11          5.4         3.7
4  setosa    12          4.8         3.4
5  setosa    13          4.8         3.0
6  setosa    14          4.3         3.0

#Species,index컬럼으로 정렬
> z<-orderBy(~Species+index,z)
> head(z)
   Species index Sepal.Length Sepal.Width
1   setosa     1          5.1         3.5
12  setosa     2          4.9         3.0
23  setosa     3          4.7         3.2
34  setosa     4          4.6         3.1
45  setosa     5          5.0         3.6
47  setosa     6          5.4         3.9

> rm(list=ls())
> x<-subset(iris,select=c(Species,Sepal.Length))
> y<-subset(iris,select=c(Species,Sepal.Width))
> z<-cbind(x,y$Sepal.Width)
> head(z)
  Species Sepal.Length y$Sepal.Width
1  setosa          5.1           3.5
2  setosa          4.9           3.0
3  setosa          4.7           3.2
4  setosa          4.6           3.1
5  setosa          5.0           3.6
6  setosa          5.4           3.9

 

반응형
Comments