data("mtcars")
mtcars <- as_tibble(mtcars)
# excel file
data <- read_excel("Salaries.xlsx")
data
## # A tibble: 397 × 6
## rank discipline yrs.since.phd yrs.service sex salary
## <chr> <chr> <dbl> <dbl> <chr> <dbl>
## 1 Prof B 19 18 Male 139750
## 2 Prof B 20 16 Male 173200
## 3 AsstProf B 4 3 Male 79750
## 4 Prof B 45 39 Male 115000
## 5 Prof B 40 41 Male 141500
## 6 AssocProf B 6 6 Male 97000
## 7 Prof B 30 23 Male 175000
## 8 Prof B 45 45 Male 147765
## 9 Prof B 21 20 Male 119250
## 10 Prof B 18 18 Female 129000
## # … with 387 more rows
Case of numeric variables
mtcars %>% map_dbl(.x = ., .f = ~mean(x = .x))
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
mtcars %>% map_dbl(.f = ~mean(x = .x))
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
mtcars %>% map_dbl(mean)
## mpg cyl disp hp drat wt qsec
## 20.090625 6.187500 230.721875 146.687500 3.596563 3.217250 17.848750
## vs am gear carb
## 0.437500 0.406250 3.687500 2.812500
# Adding an argument
mtcars %>% map_dbl(.x = ., .f = ~mean(x = .x, trim = 0.1))
## mpg cyl disp hp drat wt
## 19.6961538 6.2307692 222.5230769 141.1923077 3.5792308 3.1526923
## qsec vs am gear carb
## 17.8276923 0.4230769 0.3846154 3.6153846 2.6538462
mtcars %>% map_dbl(mean, trim = 0.1)
## mpg cyl disp hp drat wt
## 19.6961538 6.2307692 222.5230769 141.1923077 3.5792308 3.1526923
## qsec vs am gear carb
## 17.8276923 0.4230769 0.3846154 3.6153846 2.6538462
mtcars %>% select(.data = ., mpg)
## # A tibble: 32 × 1
## mpg
## <dbl>
## 1 21
## 2 21
## 3 22.8
## 4 21.4
## 5 18.7
## 6 18.1
## 7 14.3
## 8 24.4
## 9 22.8
## 10 19.2
## # … with 22 more rows
mtcars %>% select(mpg)
## # A tibble: 32 × 1
## mpg
## <dbl>
## 1 21
## 2 21
## 3 22.8
## 4 21.4
## 5 18.7
## 6 18.1
## 7 14.3
## 8 24.4
## 9 22.8
## 10 19.2
## # … with 22 more rows
Create your own function
# Halves values in columns
half_by_factor <- function(x, factor) {x / factor}
10 %>% half_by_factor(factor = 2)
## [1] 5
mtcars %>% map_dfr(.x = ., .f = ~half_by_factor(x = .x, factor = 10))
## # A tibble: 32 × 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2.1 0.6 16 11 0.39 0.262 1.65 0 0.1 0.4 0.4
## 2 2.1 0.6 16 11 0.39 0.288 1.70 0 0.1 0.4 0.4
## 3 2.28 0.4 10.8 9.3 0.385 0.232 1.86 0.1 0.1 0.4 0.1
## 4 2.14 0.6 25.8 11 0.308 0.322 1.94 0.1 0 0.3 0.1
## 5 1.87 0.8 36 17.5 0.315 0.344 1.70 0 0 0.3 0.2
## 6 1.81 0.6 22.5 10.5 0.276 0.346 2.02 0.1 0 0.3 0.1
## 7 1.43 0.8 36 24.5 0.321 0.357 1.58 0 0 0.3 0.4
## 8 2.44 0.4 14.7 6.2 0.369 0.319 2 0.1 0 0.4 0.2
## 9 2.28 0.4 14.1 9.5 0.392 0.315 2.29 0.1 0 0.4 0.2
## 10 1.92 0.6 16.8 12.3 0.392 0.344 1.83 0.1 0 0.4 0.4
## # … with 22 more rows
mtcars %>% map_dfr(half_by_factor, factor = 10)
## # A tibble: 32 × 11
## mpg cyl disp hp drat wt qsec vs am gear carb
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2.1 0.6 16 11 0.39 0.262 1.65 0 0.1 0.4 0.4
## 2 2.1 0.6 16 11 0.39 0.288 1.70 0 0.1 0.4 0.4
## 3 2.28 0.4 10.8 9.3 0.385 0.232 1.86 0.1 0.1 0.4 0.1
## 4 2.14 0.6 25.8 11 0.308 0.322 1.94 0.1 0 0.3 0.1
## 5 1.87 0.8 36 17.5 0.315 0.344 1.70 0 0 0.3 0.2
## 6 1.81 0.6 22.5 10.5 0.276 0.346 2.02 0.1 0 0.3 0.1
## 7 1.43 0.8 36 24.5 0.321 0.357 1.58 0 0 0.3 0.4
## 8 2.44 0.4 14.7 6.2 0.369 0.319 2 0.1 0 0.4 0.2
## 9 2.28 0.4 14.1 9.5 0.392 0.315 2.29 0.1 0 0.4 0.2
## 10 1.92 0.6 16.8 12.3 0.392 0.344 1.83 0.1 0 0.4 0.4
## # … with 22 more rows
When you have a grouping variable (factor)
mtcars %>% lm(formula = mpg ~ wt, data = .)
##
## Call:
## lm(formula = mpg ~ wt, data = .)
##
## Coefficients:
## (Intercept) wt
## 37.285 -5.344
mtcars %>% distinct(cyl)
## # A tibble: 3 × 1
## cyl
## <dbl>
## 1 6
## 2 4
## 3 8
reg_coeff_tbl <- mtcars %>%
# Split it into a list of data frames
split(.$cyl) %>%
# Repeat regression over each group
map(~lm(formula = mpg ~ wt, data = .x)) %>%
# Extract coefficients from regression results
map(broom::tidy, conf.int = TRUE) %>%
# Convert to tibble
bind_rows(.id = "cyl") %>%
# Filter for wt coefficients
filter(term == "wt")
reg_coeff_tbl %>%
mutate(estimate = -estimate,
conf.low = -conf.low,
conf.high = -conf.high) %>%
ggplot(aes(x = estimate, y = cyl)) +
geom_point() +
geom_errorbar(aes(xmin = conf.low, xmax = conf.high))
Choose either one of the two cases above and apply it to your data
# Turn salary from whole number to percent
salary_wholenumber_into_percentage <- function (x, factor) {x * factor}
data %>% select(salary) %>% salary_wholenumber_into_percentage(factor = 0.001)
## salary
## 1 139.750
## 2 173.200
## 3 79.750
## 4 115.000
## 5 141.500
## 6 97.000
## 7 175.000
## 8 147.765
## 9 119.250
## 10 129.000
## 11 119.800
## 12 79.800
## 13 77.700
## 14 78.000
## 15 104.800
## 16 117.150
## 17 101.000
## 18 103.450
## 19 124.750
## 20 137.000
## 21 89.565
## 22 102.580
## 23 93.904
## 24 113.068
## 25 74.830
## 26 106.294
## 27 134.885
## 28 82.379
## 29 77.000
## 30 118.223
## 31 132.261
## 32 79.916
## 33 117.256
## 34 80.225
## 35 80.225
## 36 77.000
## 37 155.750
## 38 86.373
## 39 125.196
## 40 100.938
## 41 146.500
## 42 93.418
## 43 101.299
## 44 231.545
## 45 94.384
## 46 114.778
## 47 98.193
## 48 151.768
## 49 140.096
## 50 70.768
## 51 126.621
## 52 108.875
## 53 74.692
## 54 106.639
## 55 103.760
## 56 83.900
## 57 117.704
## 58 90.215
## 59 100.135
## 60 75.044
## 61 90.304
## 62 75.243
## 63 109.785
## 64 103.613
## 65 68.404
## 66 100.522
## 67 101.000
## 68 99.418
## 69 111.512
## 70 91.412
## 71 126.320
## 72 146.856
## 73 100.131
## 74 92.391
## 75 113.398
## 76 73.266
## 77 150.480
## 78 193.000
## 79 86.100
## 80 84.240
## 81 150.743
## 82 135.585
## 83 144.640
## 84 88.825
## 85 122.960
## 86 132.825
## 87 152.708
## 88 88.400
## 89 172.272
## 90 107.008
## 91 97.032
## 92 105.128
## 93 105.631
## 94 166.024
## 95 123.683
## 96 84.000
## 97 95.611
## 98 129.676
## 99 102.235
## 100 106.689
## 101 133.217
## 102 126.933
## 103 153.303
## 104 127.512
## 105 83.850
## 106 113.543
## 107 82.099
## 108 82.600
## 109 81.500
## 110 131.205
## 111 112.429
## 112 82.100
## 113 72.500
## 114 104.279
## 115 105.000
## 116 120.806
## 117 148.500
## 118 117.515
## 119 72.500
## 120 73.500
## 121 115.313
## 122 124.309
## 123 97.262
## 124 62.884
## 125 96.614
## 126 78.162
## 127 155.500
## 128 72.500
## 129 113.278
## 130 73.000
## 131 83.001
## 132 76.840
## 133 77.500
## 134 72.500
## 135 168.635
## 136 136.000
## 137 108.262
## 138 105.668
## 139 73.877
## 140 152.664
## 141 100.102
## 142 81.500
## 143 106.608
## 144 89.942
## 145 112.696
## 146 119.015
## 147 92.000
## 148 156.938
## 149 144.651
## 150 95.079
## 151 128.148
## 152 92.000
## 153 111.168
## 154 103.994
## 155 92.000
## 156 118.971
## 157 113.341
## 158 88.000
## 159 95.408
## 160 137.167
## 161 89.516
## 162 176.500
## 163 98.510
## 164 89.942
## 165 88.795
## 166 105.890
## 167 167.284
## 168 130.664
## 169 101.210
## 170 181.257
## 171 91.227
## 172 151.575
## 173 93.164
## 174 134.185
## 175 105.000
## 176 111.751
## 177 95.436
## 178 100.944
## 179 147.349
## 180 92.000
## 181 142.467
## 182 141.136
## 183 100.000
## 184 150.000
## 185 101.000
## 186 134.000
## 187 103.750
## 188 107.500
## 189 106.300
## 190 153.750
## 191 180.000
## 192 133.700
## 193 122.100
## 194 86.250
## 195 90.000
## 196 113.600
## 197 92.700
## 198 92.000
## 199 189.409
## 200 114.500
## 201 92.700
## 202 119.700
## 203 160.400
## 204 152.500
## 205 165.000
## 206 96.545
## 207 162.200
## 208 120.000
## 209 91.300
## 210 163.200
## 211 91.000
## 212 111.350
## 213 128.400
## 214 126.200
## 215 118.700
## 216 145.350
## 217 146.000
## 218 105.350
## 219 109.650
## 220 119.500
## 221 170.000
## 222 145.200
## 223 107.150
## 224 129.600
## 225 87.800
## 226 122.400
## 227 63.900
## 228 70.000
## 229 88.175
## 230 133.900
## 231 91.000
## 232 73.300
## 233 148.750
## 234 117.555
## 235 69.700
## 236 81.700
## 237 114.000
## 238 63.100
## 239 77.202
## 240 96.200
## 241 69.200
## 242 122.875
## 243 102.600
## 244 108.200
## 245 84.273
## 246 90.450
## 247 91.100
## 248 101.100
## 249 128.800
## 250 204.000
## 251 109.000
## 252 102.000
## 253 132.000
## 254 77.500
## 255 116.450
## 256 83.000
## 257 140.300
## 258 74.000
## 259 73.800
## 260 92.550
## 261 88.600
## 262 107.550
## 263 121.200
## 264 126.000
## 265 99.000
## 266 134.800
## 267 143.940
## 268 104.350
## 269 89.650
## 270 103.700
## 271 143.250
## 272 194.800
## 273 73.000
## 274 74.000
## 275 78.500
## 276 93.000
## 277 107.200
## 278 163.200
## 279 107.100
## 280 100.600
## 281 136.500
## 282 103.600
## 283 57.800
## 284 155.865
## 285 88.650
## 286 81.800
## 287 115.800
## 288 85.000
## 289 150.500
## 290 74.000
## 291 174.500
## 292 168.500
## 293 183.800
## 294 104.800
## 295 107.300
## 296 97.150
## 297 126.300
## 298 148.800
## 299 72.300
## 300 70.700
## 301 88.600
## 302 127.100
## 303 170.500
## 304 105.260
## 305 144.050
## 306 111.350
## 307 74.500
## 308 122.500
## 309 74.000
## 310 166.800
## 311 92.050
## 312 108.100
## 313 94.350
## 314 100.351
## 315 146.800
## 316 84.716
## 317 71.065
## 318 67.559
## 319 134.550
## 320 135.027
## 321 104.428
## 322 95.642
## 323 126.431
## 324 161.101
## 325 162.221
## 326 84.500
## 327 124.714
## 328 151.650
## 329 99.247
## 330 134.778
## 331 192.253
## 332 116.518
## 333 105.450
## 334 145.098
## 335 104.542
## 336 151.445
## 337 98.053
## 338 145.000
## 339 128.464
## 340 137.317
## 341 106.231
## 342 124.312
## 343 114.596
## 344 162.150
## 345 150.376
## 346 107.986
## 347 142.023
## 348 128.250
## 349 80.139
## 350 144.309
## 351 186.960
## 352 93.519
## 353 142.500
## 354 138.000
## 355 83.600
## 356 145.028
## 357 88.709
## 358 107.309
## 359 109.954
## 360 78.785
## 361 121.946
## 362 109.646
## 363 138.771
## 364 81.285
## 365 205.500
## 366 101.036
## 367 115.435
## 368 108.413
## 369 131.950
## 370 134.690
## 371 78.182
## 372 110.515
## 373 109.707
## 374 136.660
## 375 103.275
## 376 103.649
## 377 74.856
## 378 77.081
## 379 150.680
## 380 104.121
## 381 75.996
## 382 172.505
## 383 86.895
## 384 105.000
## 385 125.192
## 386 114.330
## 387 139.219
## 388 109.305
## 389 119.450
## 390 186.023
## 391 166.605
## 392 151.292
## 393 103.106
## 394 150.564
## 395 101.738
## 396 95.329
## 397 81.035