R lapply()

1 개요[ | ]

R lapply()
x <- c(2,3,4)
unlist(lapply(x, function(n){ n*n }))
## [1]  4  9 16
x**2
## [1]  4  9 16
x <- list(1:3, 1:4, 1:5)
x
## [[1]]
## [1] 1 2 3
## 
## [[2]]
## [1] 1 2 3 4
## 
## [[3]]
## [1] 1 2 3 4 5

lapply(x, mean)
## [[1]]
## [1] 2
## 
## [[2]]
## [1] 2.5
## 
## [[3]]
## [1] 3

unlist(lapply(x, mean))
## [1] 2.0 2.5 3.0
movies <- c("SPYDERMAN","BATMAN","VERTIGO","CHINATOWN")
unlist(lapply(movies, tolower))
## [1] "spyderman" "batman"    "vertigo"   "chinatown"
unlist(lapply(movies, nchar))
## [1] 9 6 7 9

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}