ADP (R)
purrr 을 이용해야 진정한 R 사용자 !
멋쟁이천재사자
2022. 7. 17. 23:11
a <- c(1, 2, 3, 4, 5)
b <- c(1, 22, 333, 4444, 55555)
map_int(b, str_length)
## [1] 1 2 3 4 5
c <- c('abc', 'def', 'ghi')
map_chr(c, ~paste0(.x, 'z'))
## [1] "abcz" "defz" "ghiz"
d <- c(5, 4, 3, 2, 1)
map2(a, d, sum)
map2_dbl(a, d, ~.x+.y)
## [1] 6 6 6 6 6
pmap_dbl(list(a, b, d), ~..2-..1+..3)
e <- list(
list(-1, x=1, y=c(1), z='a'),
list(-2, x=4, y=c(2, 3), z='b'),
list(-3, x=9, y=c(4, 5, 6))
)
map_dbl(e, 1)
## [1] -1 -2 -3
map_chr(e, 'z', .default=NA)
## [1] "a" "b" NA