현재 작업 디렉토리에 있는 리스트 읽어오기


> list.files( )

  [1] "1-4호선수송인원연도별.csv"                                

  [2] "1-4호선승하차승객수.csv"                                  

  [3] "153번_버스_승하차현황_2014_05월.csv"                      

  [4] "153번_버스_승하차현황_2014_06월.csv"                      

(내용이 많아서 이하 내용은 생략하겠습니다 )

 

> list.files(recursive=T)       // 하위 디렉토리 내용까지 전부 출력합니다

  [1] "1-4호선수송인원연도별.csv"                                

  [2] "1-4호선승하차승객수.csv"                                  

  [3] "153번_버스_승하차현황_2014_05월.csv"                      

  [4] "153번_버스_승하차현황_2014_06월.csv"

( 내용이 많아서 이하 내용은 생략하겠습니다)



> list.files(all.files=T)         // 숨김 파일까지 전부 보여줍니다.

  [1] "."                                                        

  [2] ".."                                                       

  [3] ".a.txt"                                                   

  [4] ".Rapp.history"                                            

  [5] ".RData"

( 내용이 많아서 이하 내용은 생략하겠습니다) 








scan( ) 함수로 텍스트 파일 읽어서 배열에 저장하기


> setwd("c:\\r_temp")               //데이터가 있는 작업용 디렉터리를 설정

> scan1 <- scan('scan_1.txt')        // scan1 변수에 저장

Read 4 items

> scan1

[1] 111 222 333 444         //기본적으로 배열로 저장이 됩니다


> scan2 <- scan('scan_2.txt')

Read 4 items

> scan2

[1] 1 2 3 4


> scan2 <- scan('scan_2.txt',what="")     //문자열

Read 4 items

> scan2

[1] "1.00" "2.00" "3.00" "4.00"


> scan3 <- scan('scan_3.txt',what="")

Read 4 items

> scan3

[1] "aaa" "bbb" "ccc" "ddd"


> scan4 <- scan('scan_4.txt',what="")

Read 4 items

> scan4

[1] "aaa"  "bbb"  "111"  "2.34"






> input <- scan( )         // 숫자 입력 받기

1: 1

2: 2

3: 3

4:         // 그만하려면 여기서 그냥 엔터

Read 3 items

> input

[1] 1 2 3


> input2 <- scan(what="")         //문자 입력 받을 때는 what="" 

1: a

2: b

3: 

Read 2 items

> input2

[1] "a" "b"






readline( ) 함수로 한 줄 읽어 들이기


> input3 <- readline()

R is very fun!  <-- 이렇게 입력 후 엔터 

> input3

[1] "R is very fun!"


> input4 <- readline("Are you OK? :" )

Are you OK? : Yes!!

> input4

[1] "Yes!!"







readLines( ) 함수로 파일 읽어 들여서 배열에 담기


> input5 <- readLines('scan_4.txt')

> input5

[1] "aaa"  "bbb"  "111"  "2.34"







> fruits <- read.table('fruits.txt')

> fruits

  V1     V2    V3  V4             //라벨명

1 no   name price  qty          //헤더 부분

2  1  apple   500   5

3  2 banana   200   2

4  3  peach   200   7

5  4  berry    50   9


> fruits <- read.table('fruits.txt',header=T)         //header 가 있다고 알려주기

> fruits

  no   name price qty

1  1  apple   500   5

2  2 banana   200   2

3  3  peach   200   7

4  4  berry    50   9









> fruit2 <- read.table('fruits_2.txt')         //주석은 자동 제외

> fruit2

  V1     V2  V3 V4

1  1   apple 500  6

2  2 banana 200  2

3  3   peach 200  7

4  4   berry  50  9


> fruit2 <- read.table('fruits_2.txt',skip=2)         //두 번째 행을 건너 뜀

> fruit2

  V1     V2  V3  V4

1  2 banana 200  2

2  3  peach 200  7

3  4   berry  50   9


> fruit2 <- read.table('fruits_2.txt',nrows=2)         //두 번째 행까지 출력

> fruit2

  V1     V2  V3 V4

1  1  apple 500  6

2 banana 200  2


> fruits3 <- read.table('fruits.txt',header=T,nrows=2)       

> fruits3

  no   name price qty

1  1  apple   500   5

2  2 banana   200   2


> fruits3 <- read.table('fruits.txt',header=F,skip=2,nrows=2)       //두 번째 행 건너뛰고, 두 번째 행까지 출력

> fruits3

  V1     V2  V3 V4

1  2 banana 200  2

2  3  peach 200  7








read.csv( ) 함수로 csv 불러오기


> fruit3 <- read.csv('fruits_3.csv')

> fruit3

  no   name price pty

1  1  apple   500   6

2  2 banana   200   2

3  3  peach   200   7

4  4  berry    50   9





라벨명이 없는 경우 


> fruit4 <- read.csv('fruits_4.csv')

> fruit4

  X1  apple X500 X6        // 이 부분이 문제가 됩니다.

1  2 banana  200  2

2  3  peach  200  7

3  4  berry   50  9

 

> fruit4 <- read.csv('fruits_4.csv',header=F)

> fruit4

  V1     V2  V3 V4

1  1   apple 500  6

2  2 banana 200  2

3  3   peach 200  7

4  4   berry   50  9






라벨명을 수동으로 지정


> label <- c('NO','NAME','PRICE','QTY')

> fruit4 <- read.csv('fruits_4.csv',header=F,col.names=label)

> fruit4

  NO   NAME PRICE QTY

1  1   apple   500   6

2  2 banana   200   2

3  3  peach   200   7

4  4  berry     50   9













출처

더알음 네이버카페 : http://cafe.naver.com/theareum

R라뷰 개정판 실습데이터 모음 :  https://www.dropbox.com/s/16ytsq4q3rdaro4/%EB%8D%94%EB%A7%8E%EC%9D%B4_R%EB%9D%BC%EB%B7%B0_%EA%B0%9C%EC%A0%95%ED%8C%90%EC%9A%A9_%EC%8B%A4%EC%8A%B5%EC%9A%A9_%EB%8D%B0%EC%9D%B4%ED%84%B0_%EB%AA%A8%EC%9D%8C.zip?dl=0

+ Recent posts