[데이터프로세싱]
#subset
: 데이터를 특정조건에 해당하는 부분만 선택하는 함수
#subset
> subset(iris, Species == "setosa")
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
....
> subset(iris, Species == "setosa" & Sepal.Length > 5) #5보다 큰 애들만 골라내라!
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
11 5.4 3.7 1.5 0.2 setosa
15 5.8 4.0 1.2 0.2 setosa
16 5.7 4.4 1.5 0.4 setosa
....
> subset(iris, select = c(Sepal.Length, Species))
Sepal.Length Species
1 5.1 setosa
2 4.9 setosa
3 4.7 setosa
4 4.6 setosa
5 5.0 setosa
....
> subset(iris, select = -c(Sepal.Length, Species))
Sepal.Width Petal.Length Petal.Width
1 3.5 1.4 0.2
2 3.0 1.4 0.2
3 3.2 1.3 0.2
4 3.1 1.5 0.2
5 3.6 1.4 0.2
....
<apply 계열 함수>
: 벡터, 행렬, 데이터 프레임에 임의의 함수를 적용한 결과 리턴하는 함수
#apply(data, 1 or 2, function)
1 : 행단위
2 : 열단위
#apply
> d <- matrix(1:9,ncol=3)
> d
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> d <- matrix(1:12,nrow=3)
> d
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
> apply(d,1,sum) # 1=행의 속성
[1] 22 26 30
> apply(d,2,sum) # 2=열의 속성
[1] 6 15 24 33
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
> apply(iris[,1:4], 2, sum)
Sepal.Length Sepal.Width Petal.Length Petal.Width
876.5 458.6 563.7 179.9
#lapply
: 리스트를 반환하는 apply 함수
#lapply
> result <- lapply(1:3, function(x){x*2})
> result
[[1]]
[1] 2
[[2]]
[1] 4
[[3]]
[1] 6
> unlist(result)
[1] 2 4 6
> x <- list(a=1:3, b=4:6)
> x
$a
[1] 1 2 3
$b
[1] 4 5 6
> lapply(x,mean)
$a
[1] 2
$b
[1] 5
#unlist : 리스트를 벡터로 변환
matrix : 벡터를 행렬로 변환
as.data.frame : 행렬을 데이터 프레임으로 변환
names : colname을 읽어오거나 설정함
byrow = T : byrow를 True로 주면, 숫자를 행부터 채운다 (디폴트:열부터)
#unlist -> matrix -> data.frame -> colnames
> d <- as.data.frame(matrix(unlist(lapply(iris[,1:4],mean)),nco=4, byrow=TRUE))
> names(d) <- names(iris[,1:4])
> d
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.843333 3.057333 3.758 1.199333
(* 데이터에 missing value가 많을 경우, 평균값으로 채워넣으면 분산이 깨지지 않음)
#sapply
: 벡터, 행렬 등의 데이터 타입으로 리턴하는 apply 함수
#sapply (내부적으로 unlist를 한번 거침)
> lapply(iris[,1:4],mean)
$Sepal.Length
[1] 5.843333
$Sepal.Width
[1] 3.057333
$Petal.Length
[1] 3.758
$Petal.Width
[1] 1.199333
> class(lapply(iris[,1:4], mean))
[1] "list"
> sapply(iris[,1:4], mean)
Sepal.Length Sepal.Width Petal.Length Petal.Width
5.843333 3.057333 3.758000 1.199333
> class(sapply(iris[,1:4],mean))
[1] "numeric"
#sapply
> x <- sapply(iris[,1:4],mean)
> as.data.frame(x)
x
Sepal.Length 5.843333
Sepal.Width 3.057333
Petal.Length 3.758000
Petal.Width 1.199333
> as.data.frame(t(x))
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.843333 3.057333 3.758 1.199333
> sapply(x,class)
Sepal.Length Sepal.Width Petal.Length Petal.Width
"numeric" "numeric" "numeric" "numeric"
> sapply(iris,class)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
"numeric" "numeric" "numeric" "numeric" "factor"
> sapply(mtcars,class)
mpg cyl disp hp drat wt qsec vs am
"numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric" "numeric"
gear carb
"numeric" "numeric"
#tapply
: 그룹별로 함수를 적용하기 위한 apply 함수 (table)
#tapply
> 1:10
[1] 1 2 3 4 5 6 7 8 9 10
> rep(1,10)
[1] 1 1 1 1 1 1 1 1 1 1
> tapply(1:10, rep(1,10), sum)
1
55
> tapply(1:10, 1:10%%2==1, sum) #2로 나눈 나머지가 1인 것의 합
FALSE TRUE
30 25
> tapply(iris$Sepal.Length, iris$Species, mean)
setosa versicolor virginica
5.006 5.936 6.588
#mapply
: 다수의 인자를 적용한 apply 함수 (multiple)
#mapply
rnorm(10,0,1) #랜덤변수 10개를 뽑아라
mapply(rnorm, c(1,2,3), c(0,10,100), c(1,1,1))
mapply(mean, iris[,1:4])
(* 랜덤변수이므로, 결과값은 매번 다르다!)
#리스트의 중첩(Nested list, function)
#Nested list
> l1 <- list(c(1,2,3), c("a","b"), matrix(1:9,ncol=3))
> l1
[[1]]
[1] 1 2 3
[[2]]
[1] "a" "b"
[[3]]
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> l2 <- list(11, c(5,6,7))
> l2
[[1]]
[1] 11
[[2]]
[1] 5 6 7
> lapply(11, length)
[[1]]
[1] 1
> lapply(l2, length)
[[1]]
[1] 1
[[2]]
[1] 3
> rapply(l2, length)
[1] 1 3
> unlist(l2)
[1] 11 5 6 7
> l3 <- list(a=c(1,2,3), b=c("a","b"), c=matrix(1:9,ncol=3))
> l3
$a
[1] 1 2 3
$b
[1] "a" "b"
$c
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> unlist(l3)
a1 a2 a3 b1 b2 c1 c2 c3 c4 c5 c6 c7 c8 c9
"1" "2" "3" "a" "b" "1" "2" "3" "4" "5" "6" "7" "8" "9"
'🤖 Education > 2019AI개발자양성과정(과기부)' 카테고리의 다른 글
[LAMP] (0) | 2023.01.30 |
---|---|
[AWS] 서버열기 (0) | 2023.01.30 |
[R_STUDY] 04. Workshop (0) | 2023.01.30 |
[R_STUDY] 02. 데이터타입~프로그래밍 (0) | 2023.01.30 |
[R_STUDY] 01. 기본 ~ 데이터타입 (0) | 2023.01.30 |