>RE::VISION CRM

R 데이터 분석

0630 ... PART 3

YONG_X 2015. 6. 30. 12:52

apply, sapply, lapply 

apply plus :: http://www.r-bloggers.com/using-apply-sapply-lapply-in-r/



m <- matrix(data=cbind(rnorm(30, 0), rnorm(30, 2), rnorm(30, 


5)), nrow=30, ncol=3)



# 행평균

apply(m, 1, mean) 

# 열평균

apply(m, 2, mean)


# 자기자신? == 가로세로 평균?

apply(m, c(1,2), mean)


# UDF 활용

apply(m, 2, function(x) length(x[x<0]))



apply(m, 2, function(x) is.matrix(x))


apply(m, 2, is.vector)


apply(m, 2, length(x[x<0])) # error!!


apply(m, 2, function(x) mean(x[x>0]))




#works on a list or vector of data.

sapply(1:3, function(x) x^2)


# applications

actors <- c("hyn bin","lee na young","won bin","ha jeong woo","shin gu")

sapply(actors, function(x) substr(x, 1, 3))

sapply(actors, function(x)  substr(x, nchar(x)-2, nchar(x)))


# returns list

lapply(1:3, function(x) x^2)


sapply(1:3, function(x) x^2, simplify=F)


unlist(lapply(1:3, function(x) x^2))







apply :: http://www.r-bloggers.com/the-r-apply-function-a-tutorial-with-examples/



'R 데이터 분석' 카테고리의 다른 글

retail example  (0) 2015.07.02
경기도 교통 데이터 추가[Dataset, 작업과제]   (0) 2015.06.30
0630 파트 2  (0) 2015.06.30
[R and SQL] [0630 ...................]  (0) 2015.06.30
추가 0629  (0) 2015.06.29