Sums across columns within a row, while accounting for nonmissingness. Specify the desired columns by passing their explicit column names or by passing a regular expression to matches the column names.
row_sum(
d,
columns_to_process = character(0),
pattern = "",
new_column_name = "row_sum",
threshold_proportion = 0.75,
nonmissing_count_name = NA_character_,
verbose = FALSE
)
row_mean(
d,
columns_to_process = character(0),
pattern = "",
new_column_name = "row_mean",
threshold_proportion = 0.75,
nonmissing_count_name = NA_character_,
verbose = FALSE
)
The data.frame containing the values to sum. Required.
A character vector containing the columns
names to process (e.g., to average or to sum).
If empty, pattern
is used to select columns. Optional.
A regular expression pattern passed to base::grep()
(with perl = TRUE
). Optional
The name of the new column that represents the sum of the specified columns. Required.
Designates the minimum proportion of columns
that have a nonmissing values (within each row) in order to return a sum.
Required; defaults to to 0.75.
In other words, by default, if less than 75% of the specified
cells are missing within a row, the row sum will be NA
.
If a non-NA value is passed,
a second column will be added to d
that contains the row's count
of nonmissing items among the selected columns.
Must be a valid column name. Optional.
a logical value to designate if extra information is
displayed in the console,
such as which columns are matched by pattern
.
The data.frame d
,
with the additional column containing the row sum.
If a valid value is passed to nonmissing_count_name
,
a second column will be added as well.
If the specified columns are all logicals or integers, the new column will be an integer. Otherwise the new column will be a double.
mtcars |>
OuhscMunge::row_sum(
columns_to_process = c("cyl", "disp", "vs", "carb"),
new_column_name = "engine_sum"
)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
#> Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
#> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
#> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
#> Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
#> Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
#> Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
#> Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
#> Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
#> Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
#> Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
#> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
#> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
#> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
#> Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
#> Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
#> AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
#> Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
#> Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
#> Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
#> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
#> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
#> Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
#> Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
#> Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
#> Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
#> engine_sum
#> Mazda RX4 170.0
#> Mazda RX4 Wag 170.0
#> Datsun 710 114.0
#> Hornet 4 Drive 266.0
#> Hornet Sportabout 370.0
#> Valiant 233.0
#> Duster 360 372.0
#> Merc 240D 153.7
#> Merc 230 147.8
#> Merc 280 178.6
#> Merc 280C 178.6
#> Merc 450SE 286.8
#> Merc 450SL 286.8
#> Merc 450SLC 286.8
#> Cadillac Fleetwood 484.0
#> Lincoln Continental 472.0
#> Chrysler Imperial 452.0
#> Fiat 128 84.7
#> Honda Civic 82.7
#> Toyota Corolla 77.1
#> Toyota Corona 126.1
#> Dodge Challenger 328.0
#> AMC Javelin 314.0
#> Camaro Z28 362.0
#> Pontiac Firebird 410.0
#> Fiat X1-9 85.0
#> Porsche 914-2 126.3
#> Lotus Europa 102.1
#> Ford Pantera L 363.0
#> Ferrari Dino 157.0
#> Maserati Bora 317.0
#> Volvo 142E 128.0
mtcars |>
OuhscMunge::row_sum(
columns_to_process = c("cyl", "disp", "vs", "carb"),
new_column_name = "engine_sum",
nonmissing_count_name = "engine_nonmissing_count"
)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
#> Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
#> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
#> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
#> Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
#> Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
#> Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
#> Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
#> Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
#> Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
#> Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
#> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
#> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
#> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
#> Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
#> Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
#> AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
#> Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
#> Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
#> Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
#> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
#> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
#> Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
#> Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
#> Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
#> Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
#> engine_sum engine_nonmissing_count
#> Mazda RX4 170.0 4
#> Mazda RX4 Wag 170.0 4
#> Datsun 710 114.0 4
#> Hornet 4 Drive 266.0 4
#> Hornet Sportabout 370.0 4
#> Valiant 233.0 4
#> Duster 360 372.0 4
#> Merc 240D 153.7 4
#> Merc 230 147.8 4
#> Merc 280 178.6 4
#> Merc 280C 178.6 4
#> Merc 450SE 286.8 4
#> Merc 450SL 286.8 4
#> Merc 450SLC 286.8 4
#> Cadillac Fleetwood 484.0 4
#> Lincoln Continental 472.0 4
#> Chrysler Imperial 452.0 4
#> Fiat 128 84.7 4
#> Honda Civic 82.7 4
#> Toyota Corolla 77.1 4
#> Toyota Corona 126.1 4
#> Dodge Challenger 328.0 4
#> AMC Javelin 314.0 4
#> Camaro Z28 362.0 4
#> Pontiac Firebird 410.0 4
#> Fiat X1-9 85.0 4
#> Porsche 914-2 126.3 4
#> Lotus Europa 102.1 4
#> Ford Pantera L 363.0 4
#> Ferrari Dino 157.0 4
#> Maserati Bora 317.0 4
#> Volvo 142E 128.0 4
mtcars |>
OuhscMunge::row_mean(
columns_to_process = c("cyl", "disp", "vs", "carb"),
new_column_name = "engine_mean",
nonmissing_count_name = "engine_nonmissing_count"
)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
#> Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
#> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
#> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
#> Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
#> Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
#> Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
#> Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
#> Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
#> Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
#> Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
#> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
#> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
#> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
#> Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
#> Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
#> AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
#> Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
#> Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
#> Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
#> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
#> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
#> Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
#> Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
#> Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
#> Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
#> engine_mean engine_nonmissing_count
#> Mazda RX4 42.500 4
#> Mazda RX4 Wag 42.500 4
#> Datsun 710 28.500 4
#> Hornet 4 Drive 66.500 4
#> Hornet Sportabout 92.500 4
#> Valiant 58.250 4
#> Duster 360 93.000 4
#> Merc 240D 38.425 4
#> Merc 230 36.950 4
#> Merc 280 44.650 4
#> Merc 280C 44.650 4
#> Merc 450SE 71.700 4
#> Merc 450SL 71.700 4
#> Merc 450SLC 71.700 4
#> Cadillac Fleetwood 121.000 4
#> Lincoln Continental 118.000 4
#> Chrysler Imperial 113.000 4
#> Fiat 128 21.175 4
#> Honda Civic 20.675 4
#> Toyota Corolla 19.275 4
#> Toyota Corona 31.525 4
#> Dodge Challenger 82.000 4
#> AMC Javelin 78.500 4
#> Camaro Z28 90.500 4
#> Pontiac Firebird 102.500 4
#> Fiat X1-9 21.250 4
#> Porsche 914-2 31.575 4
#> Lotus Europa 25.525 4
#> Ford Pantera L 90.750 4
#> Ferrari Dino 39.250 4
#> Maserati Bora 79.250 4
#> Volvo 142E 32.000 4
if (require(tidyr))
tidyr::billboard |>
OuhscMunge::row_sum(
pattern = "^wk\\d{1,2}$",
new_column_name = "week_sum",
threshold_proportion = .1,
verbose = TRUE
) |>
dplyr::select(
artist,
date.entered,
week_sum,
)
#> Loading required package: tidyr
#>
#> Attaching package: ‘tidyr’
#> The following object is masked from ‘package:magrittr’:
#>
#> extract
#> The following columns will be processed:
#> - wk1
#> - wk2
#> - wk3
#> - wk4
#> - wk5
#> - wk6
#> - wk7
#> - wk8
#> - wk9
#> - wk10
#> - wk11
#> - wk12
#> - wk13
#> - wk14
#> - wk15
#> - wk16
#> - wk17
#> - wk18
#> - wk19
#> - wk20
#> - wk21
#> - wk22
#> - wk23
#> - wk24
#> - wk25
#> - wk26
#> - wk27
#> - wk28
#> - wk29
#> - wk30
#> - wk31
#> - wk32
#> - wk33
#> - wk34
#> - wk35
#> - wk36
#> - wk37
#> - wk38
#> - wk39
#> - wk40
#> - wk41
#> - wk42
#> - wk43
#> - wk44
#> - wk45
#> - wk46
#> - wk47
#> - wk48
#> - wk49
#> - wk50
#> - wk51
#> - wk52
#> - wk53
#> - wk54
#> - wk55
#> - wk56
#> - wk57
#> - wk58
#> - wk59
#> - wk60
#> - wk61
#> - wk62
#> - wk63
#> - wk64
#> - wk65
#> - wk66
#> - wk67
#> - wk68
#> - wk69
#> - wk70
#> - wk71
#> - wk72
#> - wk73
#> - wk74
#> - wk75
#> - wk76
#> # A tibble: 317 × 3
#> artist date.entered week_sum
#> <chr> <date> <dbl>
#> 1 2 Pac 2000-02-26 NA
#> 2 2Ge+her 2000-09-02 NA
#> 3 3 Doors Down 2000-04-08 1403
#> 4 3 Doors Down 2000-10-21 1342
#> 5 504 Boyz 2000-04-15 1012
#> 6 98^0 2000-08-19 753
#> 7 A*Teens 2000-07-08 NA
#> 8 Aaliyah 2000-01-29 1041
#> 9 Aaliyah 2000-03-18 533
#> 10 Adams, Yolanda 2000-08-26 1355
#> # ℹ 307 more rows
tidyr::billboard |>
OuhscMunge::row_sum(
pattern = "^wk\\d$",
new_column_name = "week_sum",
verbose = TRUE
) |>
dplyr::select(
artist,
date.entered,
week_sum,
)
#> The following columns will be processed:
#> - wk1
#> - wk2
#> - wk3
#> - wk4
#> - wk5
#> - wk6
#> - wk7
#> - wk8
#> - wk9
#> # A tibble: 317 × 3
#> artist date.entered week_sum
#> <chr> <date> <dbl>
#> 1 2 Pac 2000-02-26 598
#> 2 2Ge+her 2000-09-02 NA
#> 3 3 Doors Down 2000-04-08 567
#> 4 3 Doors Down 2000-10-21 601
#> 5 504 Boyz 2000-04-15 319
#> 6 98^0 2000-08-19 202
#> 7 A*Teens 2000-07-08 NA
#> 8 Aaliyah 2000-01-29 422
#> 9 Aaliyah 2000-03-18 259
#> 10 Adams, Yolanda 2000-08-26 606
#> # ℹ 307 more rows