갈루아의 반서재

1. 텍스트 포맷으로 저장하기

1) 엑셀의 데이터를 R 로 가져오기 위해 text 포맷으로 저장한다. 

2) 엑셀 프로그램에서 파일 > 다른이름으로저장 > 파일 형식에서 '텍스트 (탭으로 분리)' 선택

 

2. R 에서 데이터 읽기

1) read.table 을 이용해서 R 로 읽어들일 수 있다.

 

> degrees = read.table(file.choose(), sep = "\t", header = TRUE) // sep = "\t" 는 탭으로 구분된다는 의미이고, header = TRUE 는 첫번째 행에 변수 이름을 포함한다는 의미이다. 

파일을 선택하는 팝업이 뜨면 읽고자 하는 파일을 선택하면 된다. 

 

 

3. 데이터 프레임

1) 이렇게 읽어들인 값은 data frame 라는 형태로 저장된다. 

2) 데이터 프레임은 변수를 갖고 있는 컨텐이너이다.

3) 2에서 오픈한 파일의 경우 아래와 같이 XBachelor, Masters and Doctorate 의 변수를 갖고 있다.

4) 해당 데이터 프레임에서 변수 이름은 다음의 명령으로 불러낸다. 

 

> names(degrees)

 

[1] "X"         "Bachelor"  "Masters"   "Doctorate"

5) 해당 데이터 프레임은 다음과 같이 볼 수 있다.

 

> degrees

                   X           Bachelor Masters Doctorate

1           Commerce     3072     574         9

2         Humanities       3063     406       129

3      Communication     682     176        10

4                Law           521      53         3

5          Education        991     200        10

6           Sciences      1503     371        47

7           Medicine       745      52        19

8        Engineering      640      97        15

9  Resource/Planning  359      28         0

10               Art          252      31         2

11  Agriculture, Etc     243      18         0

4. 케이스네임 사용

> degrees = read.table(file.choose(), sep = "\t", header = TRUE, row.names = 1)

> degrees

                  Bachelor Masters Doctorate

Commerce              3072     574         9

Humanities            3063     406       129

Communication          682     176        10

Law                    521      53         3

Education              991     200        10

Sciences              1503     371        47

Medicine               745      52        19

Engineering            640      97        15

Resource/Planning      359      28         0

Art                    252      31         2

Agriculture, Etc       243      18         0

5. 데이터 프레임 내의 변수 억세스

$ 연산자를 통해 데이터 프레임내의 변수에 억세스할 수 있다. 

> degrees$Bachelor

[1] 3072 3063 682 521 991 1503

[7] 745 640 359 252 243


> sum(degrees$Bachelor)

[1] 12071

6. 데이터 프레임에 붙이기

데이터 프레임내의 변수에 접근하는 또 다른 방법에는 다음이 있다. 

attach 명령을 이용하는 것으로 다음과 같다. 

> attach(degrees)

> Bachelor

[1] 3072 3063 682 521 991 1503

[7] 745 640 359 252 243

> sum(Bachelor)

[1] 12071

7. 데이터 세분화

아래와 같이 subset 명령을 통해 데이터 프레임에서 조건에 맞는 부분 집합을 이끌어낼 수 있다. 

그 결과 역시 데이터 프레임이다.

> subset(degrees, Bachelor > 1000)

                  Bachelor Masters Doctorate

Commerce    3072     574          9

Humanities    3063     406       129

Sciences       1503     371         47

특정 변수만 선택해서 서브셋팅할 수 있다. 

> subset(degrees, Bachelor > 1000, select = c(Bachelor, Masters))

                   Bachelor Masters

Commerce      3072    574

Humanities      3063    406

Sciences         1503    371

8. 데이터 요약

아래와 같이 R 에는 데이터프레임의 정보를 요약해주는 summary 함수가 있다. 

> summary(degrees)

Bachelor Masters Doctorate

Min. : 243 Min. : 18.0 Min. : 0.00

1st Qu.: 440 1st Qu.: 41.5 1st Qu.: 2.50

Median : 682 Median : 97.0 Median : 10.00

Mean :1097 Mean :182.4 Mean : 22.18

3rd Qu.:1247 3rd Qu.:285.5 3rd Qu.: 17.00

Max. :3072 Max. :574.0 Max. :129.00