只在整数列上使用 purrr::map_df 函数。

huangapple go评论93阅读模式
英文:

Apply function only on integer columns using purrr::map_df

问题

这是我的代码。我需要在数据框的整数列上应用一个简单的函数,使用 purrr::map_df 函数,但我需要保留character

  1. fun1 <- function(x){(x - mean(x))/sd(x)}
  2. df <- mtcars %>% rownames_to_column()
  3. df %>% map_df(~ fun1(.x))
英文:

This is my code. I need to apply a simple function on integer columns in a dataframe using purrr::map_df function, but I need to maintain the character column:

  1. fun1 &lt;- function(x){(x - mean(x))/sd(x)}
  2. df &lt;- mtcars %&gt;% rownames_to_column()
  3. df %&gt;% map_df(~ fun1(.x))

答案1

得分: 4

你期望的输出是这样的吗?

  1. library(tidyverse)
  2. fun1 <- function(x) {
  3. (x - mean(x)) / sd(x)
  4. }
  5. df <- mtcars %>%
  6. rownames_to_column() %>%
  7. as_tibble()
  8. df %>%
  9. mutate(across(where(is.integer), fun1))
  10. # A tibble: 32 × 12
  11. rowname mpg cyl disp hp drat wt qsec vs am gear carb
  12. <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
  13. 1 Mazda RX4 0.151 -0.105 -0.571 -0.535 0.568 -0.610 -0.777 -0.868 1.19 0.424 0.735
  14. 2 Mazda RX4 Wag 0.151 -0.105 -0.571 -0.535 0.568 -0.350 -0.464 -0.868 1.19 0.424 0.735
  15. 3 Datsun 710 0.450 -1.22 -0.990 -0.783 0.474 -0.917 0.426 1.12 1.19 0.424 -1.12
  16. 4 Hornet 4 Drive 0.217 -0.105 0.220 -0.535 -0.966 -0.00230 0.890 1.12 -0.814 -0.932 -1.12
  17. 5 Hornet Sportabout -0.231 1.01 1.04 0.413 -0.835 0.228 -0.464 -0.868 -0.814 -0.932 -0.503
  18. 6 Valiant -0.330 -0.105 -0.0462 -0.608 -1.56 0.248 1.33 1.12 -0.814 -0.932 -1.12
  19. 7 Duster 360 -0.961 1.01 1.04 1.43 -0.723 0.361 -1.12 -0.868 -0.814 -0.932 0.735
  20. 8 Merc 240D 0.715 -1.22 -0.678 -1.24 0.175 -0.0278 1.20 1.12 -0.814 0.424 -0.503
  21. 9 Merc 230 0.450 -1.22 -0.726 -0.754 0.605 -0.0687 2.83 1.12 -0.814 0.424 -0.503
  22. 10 Merc 280 -0.148 -0.105 -0.509 -0.345 0.605 0.228 0.253 1.12 -0.814 0.424 0.735
  23. # … with 22 more rows
  24. # ℹ Use `print(n = ...)` to see more rows
英文:

What is your expected output exactly? Something like this?

  1. library(tidyverse)
  2. fun1 &lt;- function(x) {
  3. (x - mean(x)) / sd(x)
  4. }
  5. df &lt;- mtcars %&gt;%
  6. rownames_to_column() %&gt;%
  7. as_tibble()

  1. df %&gt;%
  2. mutate(across(where(is.integer), fun1))
  3. # A tibble: 32 &#215; 12
  4. rowname mpg cyl disp hp drat wt qsec vs am gear carb
  5. &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
  6. 1 Mazda RX4 0.151 -0.105 -0.571 -0.535 0.568 -0.610 -0.777 -0.868 1.19 0.424 0.735
  7. 2 Mazda RX4 Wag 0.151 -0.105 -0.571 -0.535 0.568 -0.350 -0.464 -0.868 1.19 0.424 0.735
  8. 3 Datsun 710 0.450 -1.22 -0.990 -0.783 0.474 -0.917 0.426 1.12 1.19 0.424 -1.12
  9. 4 Hornet 4 Drive 0.217 -0.105 0.220 -0.535 -0.966 -0.00230 0.890 1.12 -0.814 -0.932 -1.12
  10. 5 Hornet Sportabout -0.231 1.01 1.04 0.413 -0.835 0.228 -0.464 -0.868 -0.814 -0.932 -0.503
  11. 6 Valiant -0.330 -0.105 -0.0462 -0.608 -1.56 0.248 1.33 1.12 -0.814 -0.932 -1.12
  12. 7 Duster 360 -0.961 1.01 1.04 1.43 -0.723 0.361 -1.12 -0.868 -0.814 -0.932 0.735
  13. 8 Merc 240D 0.715 -1.22 -0.678 -1.24 0.175 -0.0278 1.20 1.12 -0.814 0.424 -0.503
  14. 9 Merc 230 0.450 -1.22 -0.726 -0.754 0.605 -0.0687 2.83 1.12 -0.814 0.424 -0.503
  15. 10 Merc 280 -0.148 -0.105 -0.509 -0.345 0.605 0.228 0.253 1.12 -0.814 0.424 0.735
  16. # … with 22 more rows
  17. # ℹ Use `print(n = ...)` to see more rows

答案2

得分: 3

以下是您要翻译的代码部分:

  1. library(dplyr)
  2. library(purrr)
  3. mtcars %>%
  4. select_if(is.numeric) %>%
  5. map_df(~ fun1(.)) %>%
  6. bind_cols(mtcars %>%
  7. rownames_to_column() %>%
  8. select(rowname))

带有此输出:

  1. mpg cyl disp hp drat wt qsec vs am gear carb rowname
  2. <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
  3. 1 0.151 -0.105 -0.571 -0.535 0.568 -0.610 -0.777 -0.868 1.19 0.424 0.735 Mazda RX4
  4. 2 0.151 -0.105 -0.571 -0.535 0.568 -0.350 -0.464 -0.868 1.19 0.424 0.735 Mazda RX4 Wag
  5. 3 0.450 -1.22 -0.990 -0.783 0.474 -0.917 0.426 1.12 1.19 0.424 -1.12 Datsun 710
  6. 4 0.217 -0.105 0.220 -0.535 -0.966 -0.00230 0.890 1.12 -0.814 -0.932 -1.12 Hornet 4 Drive
  7. 5 -0.231 1.01 1.04 0.413 -0.835 0.228 -0.464 -0.868 -0.814 -0.932 -0.503 Hornet Sportabout
  8. 6 -0.330 -0.105 -0.0462 -0.608 -1.56 0.248 1.33 1.12 -0.814 -0.932 -1.12 Valiant
  9. 7 -0.961 1.01 1.04 1.43 -0.723 0.361 -1.12 -0.868 -0.814 -0.932 0.735 Duster 360
  10. 8 0.715 -1.22 -0.678 -1.24 0.175 -0.0278 1.20 1.12 -0.814 0.424 -0.503 Merc 240D
  11. 9 0.450 -1.22 -0.726 -0.754 0.605 -0.0687 2.83 1.12 -0.814 0.424 -0.503 Merc 230
  12. 10 -0.148 -0.105 -0.509 -0.345 0.605 0.228 0.253 1.12 -0.814 0.424 0.735 Merc 280
  13. # … with 22 more rows
  14. # ℹ Use `print(n = ...)` to see more rows
英文:

Update(removed prior answer):

  1. library(dplyr)
  2. library(purrr)
  3. mtcars %&gt;%
  4. select_if(is.numeric) %&gt;%
  5. map_df(~ fun1(.)) %&gt;%
  6. bind_cols(mtcars %&gt;%
  7. rownames_to_column() %&gt;%
  8. select(rowname))

With this output:

  1. mpg cyl disp hp drat wt qsec vs am gear carb rowname
  2. &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt;
  3. 1 0.151 -0.105 -0.571 -0.535 0.568 -0.610 -0.777 -0.868 1.19 0.424 0.735 Mazda RX4
  4. 2 0.151 -0.105 -0.571 -0.535 0.568 -0.350 -0.464 -0.868 1.19 0.424 0.735 Mazda RX4 Wag
  5. 3 0.450 -1.22 -0.990 -0.783 0.474 -0.917 0.426 1.12 1.19 0.424 -1.12 Datsun 710
  6. 4 0.217 -0.105 0.220 -0.535 -0.966 -0.00230 0.890 1.12 -0.814 -0.932 -1.12 Hornet 4 Drive
  7. 5 -0.231 1.01 1.04 0.413 -0.835 0.228 -0.464 -0.868 -0.814 -0.932 -0.503 Hornet Sportabout
  8. 6 -0.330 -0.105 -0.0462 -0.608 -1.56 0.248 1.33 1.12 -0.814 -0.932 -1.12 Valiant
  9. 7 -0.961 1.01 1.04 1.43 -0.723 0.361 -1.12 -0.868 -0.814 -0.932 0.735 Duster 360
  10. 8 0.715 -1.22 -0.678 -1.24 0.175 -0.0278 1.20 1.12 -0.814 0.424 -0.503 Merc 240D
  11. 9 0.450 -1.22 -0.726 -0.754 0.605 -0.0687 2.83 1.12 -0.814 0.424 -0.503 Merc 230
  12. 10 -0.148 -0.105 -0.509 -0.345 0.605 0.228 0.253 1.12 -0.814 0.424 0.735 Merc 280
  13. # … with 22 more rows
  14. # ℹ Use `print(n = ...)` to see more rows

答案3

得分: 2

使用 map_if

  1. library(purrr)
  2. library(dplyr)
  3. mtcars %>%
  4. type.convert(as.is = TRUE) %>%
  5. rownames_to_column() %>%
  6. map_if(.p = ~ inherits(.x, "integer"), .f = fun1) %>%
  7. bind_cols()
英文:

Using map_if

  1. library(purrr)
  2. library(dplyr)
  3. mtcars %&gt;%
  4. type.convert(as.is = TRUE) %&gt;%
  5. rownames_to_column() %&gt;%
  6. map_if(.p = ~ inherits(.x, &quot;integer&quot;), .f = fun1) %&gt;%
  7. bind_cols()
  8. </details>
  9. # 答案4
  10. **得分**: 1
  11. 你可以选择以下方式使用`mutate_if`
  12. ``` r
  13. df %>% mutate_if(is.numeric, ~fun1(.x))

创建于2023年2月19日,使用reprex v2.0.2

英文:

you may alternatively use mutate_if as below

  1. df %&gt;% mutate_if(is.numeric, ~fun1(.x))

<sup>Created on 2023-02-19 with reprex v2.0.2</sup>

  1. rowname mpg cyl disp hp drat wt qsec
  2. 1 Mazda RX4 0.15088482 -0.1049878 -0.57061982 -0.53509284 0.56751369 -0.610399567 -0.77716515
  3. 2 Mazda RX4 Wag 0.15088482 -0.1049878 -0.57061982 -0.53509284 0.56751369 -0.349785269 -0.46378082
  4. 3 Datsun 710 0.44954345 -1.2248578 -0.99018209 -0.78304046 0.47399959 -0.917004624 0.42600682
  5. 4 Hornet 4 Drive 0.21725341 -0.1049878 0.22009369 -0.53509284 -0.96611753 -0.002299538 0.89048716
  6. 5 Hornet Sportabout -0.23073453 1.0148821 1.04308123 0.41294217 -0.83519779 0.227654255 -0.46378082
  7. 6 Valiant -0.33028740 -0.1049878 -0.04616698 -0.60801861 -1.56460776 0.248094592 1.32698675
  8. 7 Duster 360 -0.96078893 1.0148821 1.04308123 1.43390296 -0.72298087 0.360516446 -1.12412636
  9. 8 Merc 240D 0.71501778 -1.2248578 -0.67793094 -1.23518023 0.17475447 -0.027849959 1.20387148
  10. 9 Merc 230 0.44954345 -1.2248578 -0.72553512 -0.75387015 0.60491932 -0.068730634 2.82675459
  11. 10 Merc 280 -0.14777380 -0.1049878 -0.50929918 -0.34548584 0.60491932 0.227654255 0.25252621
  12. 11 Merc 280C -0.38006384 -0.1049878 -0.50929918 -0.34548584 0.60491932 0.227654255 0.58829513
  13. 12 Merc 450SE -0.61235388 1.0148821 0.36371309 0.48586794 -0.98482035 0.871524874 -0.25112717
  14. 13 Merc 450SL -0.46302456 1.0148821 0.36371309 0.48586794 -0.98482035 0.524039143 -0.13920420
  15. 14 Merc 450SLC -0.81145962 1.0148821 0.36371309 0.48586794 -0.98482035 0.575139986 0.08464175
  16. 15 Cadillac Fleetwood -1.60788262 1.0148821 1.94675381 0.85049680 -1.24665983 2.077504765 0.07344945
  17. 16 Lincoln Continental -1.60788262 1.0148821 1.84993175 0.99634834 -1.11574009 2.255335698 -0.01608893
  18. 17 Chrysler Imperial -0.89442035 1.0148821 1.68856165 1.21512565 -0.68557523 2.174596366 -0.23993487
  19. 18 Fiat 128 2.04238943 -1.2248578 -1.22658929 -1.17683962 0.90416444 -1.039646647 0.90727560
  20. 19 Honda Civic 1.71054652 -1.2248578 -1.25079481 -1.38103178 2.49390411 -1.637526508 0.37564148
  21. 20 Toyota Corolla 2.29127162 -1.2248578 -1.28790993 -1.19142477 1.16600392 -1.412682800 1.14790999
  22. 21 Toyota Corona 0.23384555 -1.2248578 -0.89255318 -0.72469984 0.19345729 -0.768812180 1.20946763
  23. 22 Dodge Challenger -0.76168319 1.0148821 0.70420401 0.04831332 -1.56460776 0.309415603 -0.54772305
  24. 23 AMC Javelin -0.81145962 1.0148821 0.59124494 0.04831332 -0.83519779 0.222544170 -0.30708866
  25. 24 Camaro Z28 -1.12671039 1.0148821 0.96239618 1.43390296 0.24956575 0.636460997 -1.36476075
  26. 25 Pontiac Firebird -0.14777380 1.0148821 1.36582144 0.41294217 -0.96611753 0.641571082 -0.44699237
  27. 26 Fiat X1-9 1.19619000 -1.2248578 -1.22416874 -1.17683962 0.90416444 -1.310481114 0.58829513
  28. 27 Porsche 914-2 0.98049211 -1.2248578 -0.89093948 -0.81221077 1.55876313 -1.100967659 -0.64285758
  29. 28 Lotus Europa 1.71054652 -1.2248578 -1.09426581 -0.49133738 0.32437703 -1.741772228 -0.53093460
  30. 29 Ford Pantera L -0.71190675 1.0148821 0.97046468 1.71102089 1.16600392 -0.048290296 -1.87401028
  31. 30 Ferrari Dino -0.06481307 -0.1049878 -0.69164740 0.41294217 0.04383473 -0.457097039 -1.31439542
  32. 31 Maserati Bora -0.84464392 1.0148821 0.56703942 2.74656682 -0.10578782 0.360516446 -1.81804880
  33. 32 Volvo 142E 0.21725341 -1.2248578 -0.88529152 -0.54967799 0.96027290 -0.446876870 0.42041067
  34. vs am gear carb
  35. 1 -0.8680278 1.1899014 0.4235542 0.7352031
  36. 2 -0.8680278 1.1899014 0.4235542 0.7352031
  37. 3 1.1160357 1.1899014 0.4235542 -1.1221521
  38. 4 1.1160357 -0.8141431 -0.9318192 -1.1221521
  39. 5 -0.8680278 -0.8141431 -0.9318192 -0.5030337
  40. 6 1.1160357 -0.8141431 -0.9318192 -1.1221521
  41. 7 -0.8680278 -0.8141431 -0.9318192 0.7352031
  42. 8 1.1160357 -0.8141431 0.4235542 -0.5030337
  43. 9 1.1160357 -0.8141431 0.4235542 -0.5030337
  44. 10 1.1160357 -0.8141431 0.4235542 0.7352031
  45. 11 1.1160357 -0.8141431 0.4235542 0.7352031
  46. 12 -0.8680278 -0.8141431 -0.9318192 0.1160847
  47. 13 -0.8680278 -0.8141431 -0.9318192 0.1160847
  48. 14 -0.8680278 -0.8141431 -0.9318192 0.1160847
  49. 15 -0.8680278 -0.8141431 -0.9318192 0.7352031
  50. 16 -0.8680278 -0.8141431 -0.9318192 0.7352031
  51. 17 -0.8680278 -0.8141431 -0.9318192 0.7352031
  52. 18 1.1160357 1.1899014 0.4235542 -1.1221521
  53. 19 1.1160357 1.1899014 0.4235542 -0.5030337
  54. 20 1.1160357 1.1899014 0.4235542 -1.1221521
  55. 21 1.1160357 -0.8141431 -0.9318192 -1.1221521
  56. 22 -0.8680278 -0.8141431 -0.9318192 -0.5030337
  57. 23 -0.8680278 -0.8141431 -0.9318192 -0.5030337
  58. 24 -0.8680278 -0.8141431 -0.9318192 0.7352031
  59. 25 -0.8680278 -0.8141431 -0.9318192 -0.5030337
  60. 26 1.1160357 1.1899014 0.4235542 -1.1221521
  61. 27 -0.8680278 1.1899014 1.7789276 -0.5030337
  62. 28 1.1160357 1.1899014 1.7789276 -0.5030337
  63. 29 -0.8680278 1.1899014 1.7789276 0.7352031
  64. 30 -0.8680278 1.1899014 1.7789276 1.9734398
  65. 31 -0.8680278 1.1899014 1.7789276 3.2116766
  66. 32 1.1160357 1.1899014 0.4235542 -0.5030337

huangapple
  • 本文由 发表于 2023年2月19日 22:41:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定