我喜欢扎克的建议
dplyr::mutate_all()
.另一个选择是
purrr::map_df()
.
scrub <- function( x ) {
x <- dplyr::if_else(dplyr::near(x, 0), 1, x)
x <- dplyr::coalesce(x, 1)
x
}
# Option 1 (These four lines are equivalent):
df %>% # This needs `library(magrittr)`
purrr::map_df(scrub)
purrr::map_df(df, scrub)
purrr::map_df(df, ~scrub(.))
purrr::map_df(df, function(x) scrub(x))
# Option 2, suggested by @zack in the comments:
dplyr::mutate_all(df, scrub)
这是结果
purr::映射df()
,这是一个
tibble
.这个
dplyr::mutate_all()。
返回
data.frame
.
# A tibble: 4 x 3
a b c
<dbl> <dbl> <dbl>
1 233 23 223
2 1 1 1
3 1 1 1
4 3455 345 30055