使用purrr::map_df仅对整数列应用函数

l2osamch  于 2023-02-20  发布在  其他
关注(0)|答案(4)|浏览(115)

这是我的代码。我需要使用purrr::map_df函数对 Dataframe 中的整数列应用一个简单的函数,* 但我需要维护character列 *:

fun1 <- function(x){(x - mean(x))/sd(x)}

df <- mtcars %>% rownames_to_column()

df %>% map_df(~  fun1(.x))
cpjpxq1n

cpjpxq1n1#

你的预期产出是什么?像这样吗?

library(tidyverse)

fun1 <- function(x) {
  (x - mean(x)) / sd(x)
}

df <- mtcars  %>%
  rownames_to_column() %>%
  as_tibble()
df %>%
  mutate(across(where(is.integer), fun1))

# A tibble: 32 × 12
   rowname              mpg    cyl    disp     hp   drat       wt   qsec     vs     am   gear   carb
   <chr>              <dbl>  <dbl>   <dbl>  <dbl>  <dbl>    <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
 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
 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
 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 
 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 
 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
 6 Valiant           -0.330 -0.105 -0.0462 -0.608 -1.56   0.248    1.33   1.12  -0.814 -0.932 -1.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
 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
 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
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
# … with 22 more rows
# ℹ Use `print(n = ...)` to see more rows
amrnrhlw

amrnrhlw2#

更新(删除之前的答案):

library(dplyr)
library(purrr)

mtcars %>% 
  select_if(is.numeric) %>% 
  map_df(~ fun1(.)) %>% 
  bind_cols(mtcars %>% 
              rownames_to_column() %>% 
              select(rowname))

使用此输出:

mpg    cyl    disp     hp   drat       wt   qsec     vs     am   gear   carb rowname          
    <dbl>  <dbl>   <dbl>  <dbl>  <dbl>    <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl> <chr>            
 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        
 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    
 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       
 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   
 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
 6 -0.330 -0.105 -0.0462 -0.608 -1.56   0.248    1.33   1.12  -0.814 -0.932 -1.12  Valiant          
 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       
 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        
 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         
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         
# … with 22 more rows
# ℹ Use `print(n = ...)` to see more rows
oalqel3c

oalqel3c3#

使用map_if

library(purrr)
library(dplyr)
mtcars %>%
   type.convert(as.is = TRUE) %>%
   rownames_to_column() %>%
   map_if(.p = ~ inherits(.x, "integer"), .f = fun1) %>%
   bind_cols()
k4emjkb1

k4emjkb14#

你可以选择使用mutate_if如下

df %>% mutate_if(is.numeric, ~fun1(.x))

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

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

相关问题