source: R을 이용한 데이터 처리 & 분석 실무, 서민구 지음
http://book.naver.com/bookdb/book_detail.nhn?bid=8317471
6. 데이터 프레임: R에서 가장 중요한 데이터 타입
> (d<-data.frame(x=c(1:5),y=c(2:10,2)))
x y
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
6 1 7
7 2 8
8 3 9
9 4 10
10 5 2
> (d<-data.frame(x=rep(1:2,times=3),y=seq(2,12,2)))
x y
1 1 2
2 2 4
3 1 6
4 2 8
5 1 10
6 2 12
>
> (d<-data.frame(x=rep(1:2,times=3),
+ y=seq(2,12,2),
+ z=c('M','F','M','F','M','F')))
x y z
1 1 2 M
2 2 4 F
3 1 6 M
4 2 8 F
5 1 10 M
6 2 12 F
>
> str(d)
'data.frame': 6 obs. of 3 variables:
$ x: int 1 2 1 2 1 2
$ y: num 2 4 6 8 10 12
$ z: Factor w/ 2 levels "F","M": 2 1 2 1 2 1
> d$x
[1] 1 2 1 2 1 2
> d$x<-rep(1:2,each=3)
> d$x
[1] 1 1 1 2 2 2
> d$w<-c("A","B","C","D","E","F")
> d
x y z w
1 1 2 M A
2 1 4 F B
3 1 6 M C
4 2 8 F D
5 2 10 M E
6 2 12 F F
> str(d)
'data.frame': 6 obs. of 4 variables:
$ x: int 1 1 1 2 2 2
$ y: num 2 4 6 8 10 12
$ z: Factor w/ 2 levels "F","M": 2 1 2 1 2 1
$ w: chr "A" "B" "C" "D" ...
>
> (x<-data.frame(1:3))
X1.3
1 1
2 2
3 3
> colnames(x)<-'val'
> x
val
1 1
2 2
3 3
> rownames(x)<-c('a','b','c')
> x
val
a 1
b 2
c 3
>
> d<-data.frame(x=c(1:5),y=c(2,4,6,8,10))
> d
x y
1 1 2
2 2 4
3 3 6
4 4 8
5 5 10
> rownames(d)<-c('a','b','c','d','e')
> d
x y
a 1 2
b 2 4
c 3 6
d 4 8
e 5 10
>
> d$x
[1] 1 2 3 4 5
> d$y
[1] 2 4 6 8 10
> d[1,]
x y
a 1 2
> d[,1]
[1] 1 2 3 4 5
> d$a
NULL
> d["a"]
Error in `[.data.frame`(d, "a") : undefined columns selected
> d["a",]
x y
a 1 2
> d
x y
a 1 2
b 2 4
c 3 6
d 4 8
e 5 10
> d[c(1,3),]
x y
a 1 2
c 3 6
> d[c(1,3),1]
[1] 1 3
> d[c(1,3),2]
[1] 2 6
>
> d[-1,]
x y
b 2 4
c 3 6
d 4 8
e 5 10
> d[-1,-2]
[1] 2 3 4 5
> d[c("x","y")]
x y
a 1 2
b 2 4
c 3 6
d 4 8
e 5 10
> d[,c("x","y")]
x y
a 1 2
b 2 4
c 3 6
d 4 8
e 5 10
> d[c("x","y"),]
x y
NA NA NA
NA.1 NA NA
> d[,"x"]
[1] 1 2 3 4 5
> d[,c("x")]
[1] 1 2 3 4 5
> d[,c("x"),drop=F]
x
a 1
b 2
c 3
d 4
e 5
> (d<-data.frame(a=1:3,b=4:6,c=7:9))
a b c
1 1 4 7
2 2 5 8
3 3 6 9
> d[,names(d)%in%c("b","c")]
b c
1 4 7
2 5 8
3 6 9
> d[,!names(d)%in%c("b","c")]
[1] 1 2 3
> d[,!names(d)%in%c("b","c",drop=F)]
[1] 1 2 3
> View(d)
7. 타입 판별
> class(c(1,2))
[1] "numeric"
> class(matrix(c(1,2)))
[1] "matrix"
> class(data.frame(x=c(1,2),y=c(3,4)))
[1] "data.frame"
> str(c(1,2))
num [1:2] 1 2
> str(matrix(c(1,2)))
num [1:2, 1] 1 2
> str(list(c(1,2)))
List of 1
$ : num [1:2] 1 2
> str(data.frame(x=c(1,2)))
'data.frame': 2 obs. of 1 variable:
$ x: num 1 2
> is.factor(factor(c("m","f")))
[1] TRUE
> is.numeric(c(1:5))
[1] TRUE
> is.character(c("a","b"))
[1] TRUE
> is.data.frame(data.frame(x=1:6))
[1] TRUE
8. 타입 변환
> y<-c("a","b","c")
> as.factor(y)
[1] a b c
Levels: a b c
> y
[1] "a" "b" "c"
> as.character(as.factor(y))
[1] "a" "b" "c"
> x<-data.frame(1:9,ncol=3)
> x
X1.9 ncol
1 1 3
2 2 3
3 3 3
4 4 3
5 5 3
6 6 3
7 7 3
8 8 3
9 9 3
> is.data.frame(x)
[1] TRUE
> is.matrix(x)
[1] FALSE
> x<-matrix(1:9,ncol=3)
> x
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> is.matrix(x)
[1] TRUE
> as.data.frame(x)
V1 V2 V3
1 1 4 7
2 2 5 8
3 3 6 9
> is.data.frame(x)
[1] FALSE
> (x<-data.frame(matrix(c(1:4),ncol=2)))
X1 X2
1 1 3
2 2 4
> list(x=c(1,2),y=c(3,4))
$x
[1] 1 2
$y
[1] 3 4
> data.frame(list(x=c(1,2),y=c(3,4)))
x y
1 1 3
2 2 4
> factor(c("m","f"),levels=c("m","f"))
[1] m f
Levels: m f
'Python, R 분석과 프로그래밍' 카테고리의 다른 글
dim, str, plot (0) | 2015.10.29 |
---|---|
도수분포표와 히스토그램 (0) | 2015.08.12 |
[데이터 처리 & 분석 실무] 데이터 타입 - 스칼라, 벡터, 리스트, 행렬, 배열 (0) | 2015.02.12 |
R Practice - diet_data (0) | 2015.01.19 |
[텍스트 마이닝 연습] 2014 대통령 신년사 분석 (0) | 2015.01.14 |