#######2014.12.24.수###########

x<-scan()

 x<-scan()
1: 1
2: 3
3: 5
4: 7
5: 9
6:
Read 5 items
> x
[1] 1 3 5 7 9


y<-scan(what="character")     # scan(옵션), 1 : 2: 3: 에 입력
1: ab  
2: cd
3: ef
> y
[1] "ab" "cd" "ef"


z<-matrix(scan(),nrow=2)
> z<-matrix(scan(),nrow=2)
1: 12
2: 23
3: 56
4: 12
> z
     [,1] [,2]
[1,]    2    4
[2,]    3    5


xyz=data.frame() #

xyz
data frame with 0 columns and 0 rows  # 빈 데이터 프레임

xyz=edit(xyz)#엑셀 같은 창이 생성됨 # 변수, 데이터 입력가능

xyz # 출력
 
 age gender income
1  24      M   2000
2  35      F   3100
3  28      F   3800
4  21      F   2800

# 시험은 외부파일 불러오기 : 텍스트파일 or 엑셀파일

x<-scan("D:/R/data1.txt")     #txt파일 읽기, 경로만지정, 스캔 1: 에다가 입력
 x<-scan()
1:


xyz<-read.table("D:/R/data2_1.txt")   # 2차원 텍스트파일 읽기
 V1 V2   V3
1 24  M 2000
2 35  F 3100
3 28  F 3800
4 21  F 2800

colnames(xyz)<-c("age","gender","income")  # collumn에 이름붙이기(변수명)
 xyz
  age gender income
1  24      M   2000
2  35      F   3100
3  28      F   3800
4  21      F   2800

xyz<-read.table("D:/R/data2_2.txt", header=T)  #변수명 header=TRUE
  age gender income
1  24      M   2000
2  35      F   3100
3  28      F   3800
4  21      F   2800

 

> class(xyz$gender)        #속성 확인해보기
[1] "factor"
> class(xyz$age)
[1] "integer"
> class(xyz$income)
[1] "integer"
>


xyz<-read.table("D:/R/data2_2.txt", header=TRUE, stringAsFactor=FALSE)
#factor로 인식하지 못하게 하기?

 

xyz<-read.table("D:/R/data2_4.txt", header=TRUE, na.strings=".")
#결측값 처리 NA-숫자 결측값, <NA>-문자결측값 , na.strings="결측값에 해당하는 기호" 

 

xyz
  age gender income
1  24      M   2000
2  NA      F   3100
3  28   <NA>   3800
4  21      F   2800
>

 

xyz<-read.table("D:/R/data3.txt", header=TRUE, sep=",")
#콤마로 구분된 파일 csv파일, sep="구분방식" 여기선 콤마? / ,:구분자,delimete
 xyz
  age gender income
1  24      M   2000
2  35      F   3100
3  28      F   3800
4  21      F   2800

 

xyz<-read.csv("D:/R/data5.txt")  #csv 파일 읽기
> xyz
  age gender income
1  24      M   2000
2  35      F   3100
3  28      F   3800
4  21      F   2800

 

##설문지 코딩
data6=read.fwf("D:/R/data6.txt",widths=c(3,1,2,1,1))  #widths=c(변수당자릿수)
> data6
  V1 V2 V3 V4 V5
1  1  1 25  1  1
2  2  2 30  3  3
3  3  1 35  3  5
4  4  2 42  2  4
5  5  1 28  4  5
>


colnames(data6)<-c("id","gender","age","job","sat") #columm이름 변수명넣기
> data6
  id gender age job sat
1  1      1  25   1   1
2  2      2  30   3   3
3  3      1  35   3   5
4  4      2  42   2   4
5  5      1  28   4   5
>

install.packages("xlsx") #xlsx 페키지 설치
library(xlsx)  #xlsx 실행준비
 xyz<-read.xlsx("D:/R/data6.xlsx", 1,header=F)   # ,1은 시트1 의미 xlsx파일읽기

> xyz
  X1 X2 X3 X4 X5
1  1  1 25  1  1
2  2  2 30  3  3
3  3  1 35  3  5
4  4  2 42  2  4
5  5  1 28  4  5

colnames(xyz)<-c("id","gender","age","job","sat")  #columm 이름넣기

> xyz
  id gender age job sat
1  1      1  25   1   1
2  2      2  30   3   3
3  3      1  35   3   5
4  4      2  42   2   4
5  5      1  28   4   5

 

 

 

 

 

.RData

 

data1.txt

 

data2_1.txt

 

data2_2.txt

 

data2_4.txt

 

data3.txt

 

data5.txt

 

data6.txt

 

data6.xlsx

 

program1.R

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'프로그래밍, 통계학 > R(전산실습)' 카테고리의 다른 글

2014.12.30.화요일  (0) 2014.12.30
14.12.29 월요일 전산실습  (0) 2014.12.29
14.12.26 금요일 전산실습  (0) 2014.12.26
2014.12.23(결석수업)자료  (0) 2014.12.24
2014.12.22.월요일  (0) 2014.12.22
블로그 이미지

테시리

,