Extension of the dplyr::mutate function that allows the user to mutate only a specific filtered subset of a data, while leaving the other parts of the data intact
Arguments
- .
data object
- sub.set
subset of data to modify
- ...
mutation syntax similar to dplyr::mutate
Examples
#mutate a subsection filter of mtcars
dt = mtcars
names(dt)
#> [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
#> [11] "carb"
head(dt)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
mutate_filter(dt,mpg == 21.0 & cyl == 6, cyl=1000,hp=2000,vs=hp*2)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21.0 1000 160.0 2000 3.90 2.620 16.46 220 1 4 4
#> Mazda RX4 Wag 21.0 1000 160.0 2000 3.90 2.875 17.02 220 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
dt2 = beaver1
names(dt2)
#> [1] "day" "time" "temp" "activ"
head(dt2)
#> day time temp activ
#> 1 346 840 36.33 0
#> 2 346 850 36.34 0
#> 3 346 900 36.35 0
#> 4 346 910 36.42 0
#> 5 346 920 36.55 0
#> 6 346 930 36.69 0
mutate_filter(dt2, day == 346 & time < 1200, activ = 12, temp = round(temp*10,1))
#> day time temp activ
#> 1 346 840 363.30 12
#> 2 346 850 363.40 12
#> 3 346 900 363.50 12
#> 4 346 910 364.20 12
#> 5 346 920 365.50 12
#> 6 346 930 366.90 12
#> 7 346 940 367.10 12
#> 8 346 950 367.50 12
#> 9 346 1000 368.10 12
#> 10 346 1010 368.80 12
#> 11 346 1020 368.90 12
#> 12 346 1030 369.10 12
#> 13 346 1040 368.50 12
#> 14 346 1050 368.90 12
#> 15 346 1100 368.90 12
#> 16 346 1110 366.70 12
#> 17 346 1120 365.00 12
#> 18 346 1130 367.40 12
#> 19 346 1140 367.70 12
#> 20 346 1150 367.60 12
#> 21 346 1200 36.78 0
#> 22 346 1210 36.82 0
#> 23 346 1220 36.89 0
#> 24 346 1230 36.99 0
#> 25 346 1240 36.92 0
#> 26 346 1250 36.99 0
#> 27 346 1300 36.89 0
#> 28 346 1310 36.94 0
#> 29 346 1320 36.92 0
#> 30 346 1330 36.97 0
#> 31 346 1340 36.91 0
#> 32 346 1350 36.79 0
#> 33 346 1400 36.77 0
#> 34 346 1410 36.69 0
#> 35 346 1420 36.62 0
#> 36 346 1430 36.54 0
#> 37 346 1440 36.55 0
#> 38 346 1450 36.67 0
#> 39 346 1500 36.69 0
#> 40 346 1510 36.62 0
#> 41 346 1520 36.64 0
#> 42 346 1530 36.59 0
#> 43 346 1540 36.65 0
#> 44 346 1550 36.75 0
#> 45 346 1600 36.80 0
#> 46 346 1610 36.81 0
#> 47 346 1620 36.87 0
#> 48 346 1630 36.87 0
#> 49 346 1640 36.89 0
#> 50 346 1650 36.94 0
#> 51 346 1700 36.98 0
#> 52 346 1710 36.95 0
#> 53 346 1720 37.00 0
#> 54 346 1730 37.07 1
#> 55 346 1740 37.05 0
#> 56 346 1750 37.00 0
#> 57 346 1800 36.95 0
#> 58 346 1810 37.00 0
#> 59 346 1820 36.94 0
#> 60 346 1830 36.88 0
#> 61 346 1840 36.93 0
#> 62 346 1850 36.98 0
#> 63 346 1900 36.97 0
#> 64 346 1910 36.85 0
#> 65 346 1920 36.92 0
#> 66 346 1930 36.99 0
#> 67 346 1940 37.01 0
#> 68 346 1950 37.10 1
#> 69 346 2000 37.09 0
#> 70 346 2010 37.02 0
#> 71 346 2020 36.96 0
#> 72 346 2030 36.84 0
#> 73 346 2040 36.87 0
#> 74 346 2050 36.85 0
#> 75 346 2100 36.85 0
#> 76 346 2110 36.87 0
#> 77 346 2120 36.89 0
#> 78 346 2130 36.86 0
#> 79 346 2140 36.91 0
#> 80 346 2150 37.53 1
#> 81 346 2200 37.23 0
#> 82 346 2210 37.20 0
#> 83 346 2230 37.25 1
#> 84 346 2240 37.20 0
#> 85 346 2250 37.21 0
#> 86 346 2300 37.24 1
#> 87 346 2310 37.10 0
#> 88 346 2320 37.20 0
#> 89 346 2330 37.18 0
#> 90 346 2340 36.93 0
#> 91 346 2350 36.83 0
#> 92 347 0 36.93 0
#> 93 347 10 36.83 0
#> 94 347 20 36.80 0
#> 95 347 30 36.75 0
#> 96 347 40 36.71 0
#> 97 347 50 36.73 0
#> 98 347 100 36.75 0
#> 99 347 110 36.72 0
#> 100 347 120 36.76 0
#> 101 347 130 36.70 0
#> 102 347 140 36.82 0
#> 103 347 150 36.88 0
#> 104 347 200 36.94 0
#> 105 347 210 36.79 0
#> 106 347 220 36.78 0
#> 107 347 230 36.80 0
#> 108 347 240 36.82 0
#> 109 347 250 36.84 0
#> 110 347 300 36.86 0
#> 111 347 310 36.88 0
#> 112 347 320 36.93 0
#> 113 347 330 36.97 0
#> 114 347 340 37.15 1