dplyr >= 1.0.0
_if
以, _at
,结尾的作用域动词已被1.0.0 或更高版本中_all
的使用所取代。为此,请使用:across()
packageVersion("dplyr")
across
df %>%
mutate(across(where(is.character), toupper))
- 第一个参数是使用tidyselect语法
across
转换哪些列。以上将在所有字符列中应用该函数。
- 第二个参数
across
是要应用的函数。这也支持 lambda 风格的语法:~ toupper(.x)
这使得设置额外的函数参数变得简单明了。
数据
df <- structure(list(city = c("Austin", "Austin", "Austin", "Austin",
"Austin", "Austin", "Austin", "Austin", "Austin", "Austin"),
hs_cd = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L), sl_no = c(2L,
3L, 4L, 5L, 2L, 1L, 3L, 4L, 3L, 1L), col_01 = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), col_02 = c(46L, 32L, 27L, 20L,
42L, 52L, 25L, 22L, 30L, 65L), col_03 = c("Female", "Male",
"Male", "Female", "Female", "Male", "Male", "Female", "Female",
"Female")), class = "data.frame", row.names = c(NA, -10L))