繁体   English   中英

mutate_at 在具有不同功能的多组列上

[英]mutate_at on multiple sets of columns with different functions

我定义了必须应用于不同组 dataframe 列的函数。 例如, mtcars我想将as.integer() function 应用于列c("mpg", "cyl")as.logical()c("vs", "am")

library(dplyr)

mtcars %>% 
  mutate_at(c("mpg", "cyl"), as.integer) %>% 
  mutate_at(c("vs", "am"), as.logical)

有什么做法,最好使用 tidyverse,用相应的函数保存此列集并应用它们而不使用mutate_at多次。

这是我接近它的方式。 结果是可用于进一步覆盖现有列或创建新列或用作独立数据 object 的矩阵列表。

vars <- list(van = c("mpg", "cyl"),
             tu = c("vs", "am"))
funk <- list(van = as.integer,
             tu = as.logical)

mapply(FUN = function(v, f) {
  sapply(mtcars[, v], FUN = f)
}, v = vars, f = funk, SIMPLIFY = FALSE)

$van
      mpg cyl
 [1,]  21   6
 [2,]  21   6
 [3,]  22   4
 [4,]  21   6
 [5,]  18   8
 ...
$tu
         vs    am
 [1,] FALSE  TRUE
 [2,] FALSE  TRUE
 [3,]  TRUE  TRUE
 [4,]  TRUE FALSE
 [5,] FALSE FALSE
 ...

要覆盖现有列,您可以使用“可怕的”for 循环。 :)

mtcars[colnames(out$van)] <- out$van
mtcars[colnames(out$tu)] <- out$tu
# in generalized form
for (i in seq_along(out)) {
  mtcars[colnames(out[[i]])] <- out[[i]]
}

> head(mtcars)
                  mpg cyl disp  hp drat    wt  qsec    vs    am gear carb
Mazda RX4          21   6  160 110 3.90 2.620 16.46 FALSE  TRUE    4    4
Mazda RX4 Wag      21   6  160 110 3.90 2.875 17.02 FALSE  TRUE    4    4
Datsun 710         22   4  108  93 3.85 2.320 18.61  TRUE  TRUE    4    1
Hornet 4 Drive     21   6  258 110 3.08 3.215 19.44  TRUE FALSE    3    1
Hornet Sportabout  18   8  360 175 3.15 3.440 17.02 FALSE FALSE    3    2
Valiant            18   6  225 105 2.76 3.460 20.22  TRUE FALSE    3    1

或者在一个循环中完成所有事情(更短)。

for (i in seq_along(vars)) {
  cls <- vars[[i]]
  f <- funk[[i]]

  mtcars[, cls] <- sapply(mtcars[, cls], FUN = f)
}

> head(mtcars)
                  mpg cyl disp  hp drat    wt  qsec    vs    am gear carb
Mazda RX4          21   6  160 110 3.90 2.620 16.46 FALSE  TRUE    4    4
Mazda RX4 Wag      21   6  160 110 3.90 2.875 17.02 FALSE  TRUE    4    4
Datsun 710         22   4  108  93 3.85 2.320 18.61  TRUE  TRUE    4    1
Hornet 4 Drive     21   6  258 110 3.08 3.215 19.44  TRUE FALSE    3    1
Hornet Sportabout  18   8  360 175 3.15 3.440 17.02 FALSE FALSE    3    2
Valiant            18   6  225 105 2.76 3.460 20.22  TRUE FALSE    3    1

我正要提出@Roman Lustrik 在他答案的最后一部分中使用的相同方法,但这是在我打字之间完成的:)。 当我在这里时,我想我可以给 R 的 switch() function 一些爱,它也可以完成这项工作。

for (i in colnames(mtcars)) {
    mtcars[, i] = switch(i,
                         mpg = as.integer(mtcars[, i]),
                         cyl = as.integer(mtcars[, i]),
                         vs = as.logical(mtcars[, i]),
                         am = as.logical(mtcars[, i]))
}

> head(mtcars)
                  mpg cyl    vs    am
Mazda RX4          21   6 FALSE  TRUE
Mazda RX4 Wag      21   6 FALSE  TRUE
Datsun 710         22   4  TRUE  TRUE
Hornet 4 Drive     21   6  TRUE FALSE
Hornet Sportabout  18   8 FALSE FALSE
Valiant            18   6  TRUE FALSE

编辑:

由于 switch() function 如果没有给出默认值,则会产生删除列的副作用,并且 OP 要求保留所有列......这是解决方案:

for (i in colnames(mtcars)) {
    mtcars[, i] = switch(i,
                         mpg = as.integer(mtcars[, i]),
                         cyl = as.integer(mtcars[, i]),
                         vs = as.logical(mtcars[, i]),
                         am = as.logical(mtcars[, i]),
                         mtcars[, i]) # just add a default option
}

> head(mtcars)
                  mpg cyl disp  hp drat    wt  qsec    vs    am gear carb
Mazda RX4          21   6  160 110 3.90 2.620 16.46 FALSE  TRUE    4    4
Mazda RX4 Wag      21   6  160 110 3.90 2.875 17.02 FALSE  TRUE    4    4
Datsun 710         22   4  108  93 3.85 2.320 18.61  TRUE  TRUE    4    1
Hornet 4 Drive     21   6  258 110 3.08 3.215 19.44  TRUE FALSE    3    1
Hornet Sportabout  18   8  360 175 3.15 3.440 17.02 FALSE FALSE    3    2
Valiant            18   6  225 105 2.76 3.460 20.22  TRUE FALSE    3    1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM