Home > OS >  Renaming a variable and conditionally changing the values in R (using the dplyr package)
Renaming a variable and conditionally changing the values in R (using the dplyr package)

Time:09-23

I am trying to use the rename() function of the dplyr package to change the variable mpg to mpgclass:

library(tidyverse)

Auto.df <- as_tibble(Auto.df)

rename(Auto.df, mpgclass = mpg)

This executes without any errors, but it does not actually rename the variable, as can be seen here:

     mpg cylinders displacement horsepower weight acceleration  year origin name                  
   <dbl>     <dbl>        <dbl>      <dbl>  <dbl>        <dbl> <dbl>  <dbl> <fct>                 
 1    18         8          307        130   3504         12      70      1 chevrolet chevelle ma…
 2    15         8          350        165   3693         11.5    70      1 buick skylark 320     
 3    18         8          318        150   3436         11      70      1 plymouth satellite    
 4    16         8          304        150   3433         12      70      1 amc rebel sst         
 5    17         8          302        140   3449         10.5    70      1 ford torino           
 6    15         8          429        198   4341         10      70      1 ford galaxie 500      
 7    14         8          454        220   4354          9      70      1 chevrolet impala      
 8    14         8          440        215   4312          8.5    70      1 plymouth fury iii     
 9    14         8          455        225   4425         10      70      1 pontiac catalina      
10    15         8          390        190   3850          8.5    70      1 amc ambassador dpl    

I then want to conditionally rename the observations in mpgclass, kind of like this in base R:

Auto.df$mpgclass <- Auto.df$mpgclass[Auto.df$mpgclass < 20] <- 'low'
Auto.df$mpgclass <- Auto.df$mpgclass[20 <= Auto.df$mpgclass & Auto.df$mpg < 27] <- 'medium'
Auto.df$mpgclass <- Auto.df$mpgclass[27 <= Auto.df$mpgclass] <- 'high'

However, I can't figure out how to do this using the dplyr package.

How do I do this?

My data is as follows:

structure(list(mpg = c(18, 15, 18, 16, 17, 15, 14, 14, 14, 15, 
15, 14, 15, 14, 24, 22, 18, 21, 27, 26, 25, 24, 25, 26, 21, 10, 
10, 11, 9, 27, 28, 25, 19, 16, 17, 19, 18, 14, 14, 14, 14, 12, 
13, 13, 18, 22, 19, 18, 23, 28, 30, 30, 31, 35, 27, 26, 24, 25, 
23, 20, 21, 13, 14, 15, 14, 17, 11, 13, 12, 13, 19, 15, 13, 13, 
14, 18, 22, 21, 26, 22, 28, 23, 28, 27, 13, 14, 13, 14, 15, 12, 
13, 13, 14, 13, 12, 13, 18, 16, 18, 18, 23, 26, 11, 12, 13, 12, 
18, 20, 21, 22, 18, 19, 21, 26, 15, 16, 29, 24, 20, 19, 15, 24, 
20, 11, 20, 19, 15, 31, 26, 32, 25, 16, 16, 18, 16, 13, 14, 14, 
14, 29, 26, 26, 31, 32, 28, 24, 26, 24, 26, 31, 19, 18, 15, 15, 
16, 15, 16, 14, 17, 16, 15, 18, 21, 20, 13, 29, 23, 20, 23, 24, 
25, 24, 18, 29, 19, 23, 23, 22, 25, 33, 28, 25, 25, 26, 27, 17.5, 
16, 15.5, 14.5, 22, 22, 24, 22.5, 29, 24.5, 29, 33, 20, 18, 18.5, 
17.5, 29.5, 32, 28, 26.5, 20, 13, 19, 19, 16.5, 16.5, 13, 13, 
13, 31.5, 30, 36, 25.5, 33.5, 17.5, 17, 15.5, 15, 17.5, 20.5, 
19, 18.5, 16, 15.5, 15.5, 16, 29, 24.5, 26, 25.5, 30.5, 33.5, 
30, 30.5, 22, 21.5, 21.5, 43.1, 36.1, 32.8, 39.4, 36.1, 19.9, 
19.4, 20.2, 19.2, 20.5, 20.2, 25.1, 20.5, 19.4, 20.6, 20.8, 18.6, 
18.1, 19.2, 17.7, 18.1, 17.5, 30, 27.5, 27.2, 30.9, 21.1, 23.2, 
23.8, 23.9, 20.3, 17, 21.6, 16.2, 31.5, 29.5, 21.5, 19.8, 22.3, 
20.2, 20.6, 17, 17.6, 16.5, 18.2, 16.9, 15.5, 19.2, 18.5, 31.9, 
34.1, 35.7, 27.4, 25.4, 23, 27.2, 23.9, 34.2, 34.5, 31.8, 37.3, 
28.4, 28.8, 26.8, 33.5, 41.5, 38.1, 32.1, 37.2, 28, 26.4, 24.3, 
19.1, 34.3, 29.8, 31.3, 37, 32.2, 46.6, 27.9, 40.8, 44.3, 43.4, 
36.4, 30, 44.6, 33.8, 29.8, 32.7, 23.7, 35, 32.4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6, 8, 6, 8, 8, 
4, 4, 4, 4, 4, 4, 4, 4, 5, 6, 4, 6, 4, 4, 6, 6, 4, 6, 6, 8, 8, 
8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 5, 8, 4, 8, 4, 4, 4, 4, 4, 6, 6, 
4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 4, 
4, 4, 4, 6, 3, 4, 4, 4, 4, 4, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 8, 6, 6, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 4, 6, 4, 4, 4, 4, 4, 4, 
4, 4), displacement = c(307, 350, 318, 304, 302, 429, 454, 440, 
455, 390, 383, 340, 400, 455, 113, 198, 199, 200, 97, 97, 110, 
107, 104, 121, 199, 360, 307, 318, 304, 97, 140, 113, 232, 225, 
250, 250, 232, 350, 400, 351, 318, 383, 400, 400, 258, 140, 250, 
250, 122, 116, 79, 88, 71, 72, 97, 91, 113, 97.5, 97, 140, 122, 
350, 400, 318, 351, 304, 429, 350, 350, 400, 70, 304, 307, 302, 
318, 121, 121, 120, 96, 122, 97, 120, 98, 97, 350, 304, 350, 
302, 318, 429, 400, 351, 318, 440, 455, 360, 225, 250, 232, 250, 
198, 97, 400, 400, 360, 350, 232, 97, 140, 108, 70, 122, 155, 
98, 350, 400, 68, 116, 114, 121, 318, 121, 156, 350, 198, 232, 
250, 79, 122, 71, 140, 250, 258, 225, 302, 350, 318, 302, 304, 
98, 79, 97, 76, 83, 90, 90, 116, 120, 108, 79, 225, 250, 250, 
250, 400, 350, 318, 351, 231, 250, 258, 225, 231, 262, 302, 97, 
140, 232, 140, 134, 90, 119, 171, 90, 232, 115, 120, 121, 121, 
91, 107, 116, 140, 98, 101, 305, 318, 304, 351, 225, 250, 200, 
232, 85, 98, 90, 91, 225, 250, 250, 258, 97, 85, 97, 140, 130, 
318, 120, 156, 168, 350, 350, 302, 318, 98, 111, 79, 122, 85, 
305, 260, 318, 302, 250, 231, 225, 250, 400, 350, 400, 351, 97, 
151, 97, 140, 98, 98, 97, 97, 146, 121, 80, 90, 98, 78, 85, 91, 
260, 318, 302, 231, 200, 200, 140, 225, 232, 231, 200, 225, 258, 
305, 231, 302, 318, 98, 134, 119, 105, 134, 156, 151, 119, 131, 
163, 121, 163, 89, 98, 231, 200, 140, 232, 225, 305, 302, 351, 
318, 350, 351, 267, 360, 89, 86, 98, 121, 183, 350, 141, 260, 
105, 105, 85, 91, 151, 173, 173, 151, 98, 89, 98, 86, 151, 140, 
151, 225, 97, 134, 120, 119, 108, 86, 156, 85, 90, 90, 121, 146, 
91, 97, 89, 168, 70, 122, 107, 135, 151, 156, 173, 135, 79, 86, 
81, 97, 85, 89, 91, 105, 98, 98, 105, 107, 108, 119, 120, 141, 
145, 168, 146, 231, 350, 200, 225, 112, 112, 112, 112, 135, 151, 
140, 105, 91, 91, 105, 98, 120, 107, 108, 91, 91, 91, 181, 262, 
156, 232, 144, 135, 151, 140, 97, 135, 120, 119), horsepower = c(130, 
165, 150, 150, 140, 198, 220, 215, 225, 190, 170, 160, 150, 225, 
95, 95, 97, 85, 88, 46, 87, 90, 95, 113, 90, 215, 200, 210, 193, 
88, 90, 95, 100, 105, 100, 88, 100, 165, 175, 153, 150, 180, 
170, 175, 110, 72, 100, 88, 86, 90, 70, 76, 65, 69, 60, 70, 95, 
80, 54, 90, 86, 165, 175, 150, 153, 150, 208, 155, 160, 190, 
97, 150, 130, 140, 150, 112, 76, 87, 69, 86, 92, 97, 80, 88, 
175, 150, 145, 137, 150, 198, 150, 158, 150, 215, 225, 175, 105, 
100, 100, 88, 95, 46, 150, 167, 170, 180, 100, 88, 72, 94, 90, 
85, 107, 90, 145, 230, 49, 75, 91, 112, 150, 110, 122, 180, 95, 
100, 100, 67, 80, 65, 75, 100, 110, 105, 140, 150, 150, 140, 
150, 83, 67, 78, 52, 61, 75, 75, 75, 97, 93, 67, 95, 105, 72, 
72, 170, 145, 150, 148, 110, 105, 110, 95, 110, 110, 129, 75, 
83, 100, 78, 96, 71, 97, 97, 70, 90, 95, 88, 98, 115, 53, 86, 
81, 92, 79, 83, 140, 150, 120, 152, 100, 105, 81, 90, 52, 60, 
70, 53, 100, 78, 110, 95, 71, 70, 75, 72, 102, 150, 88, 108, 
120, 180, 145, 130, 150, 68, 80, 58, 96, 70, 145, 110, 145, 130, 
110, 105, 100, 98, 180, 170, 190, 149, 78, 88, 75, 89, 63, 83, 
67, 78, 97, 110, 110, 48, 66, 52, 70, 60, 110, 140, 139, 105, 
95, 85, 88, 100, 90, 105, 85, 110, 120, 145, 165, 139, 140, 68, 
95, 97, 75, 95, 105, 85, 97, 103, 125, 115, 133, 71, 68, 115, 
85, 88, 90, 110, 130, 129, 138, 135, 155, 142, 125, 150, 71, 
65, 80, 80, 77, 125, 71, 90, 70, 70, 65, 69, 90, 115, 115, 90, 
76, 60, 70, 65, 90, 88, 90, 90, 78, 90, 75, 92, 75, 65, 105, 
65, 48, 48, 67, 67, 67, 67, 62, 132, 100, 88, 72, 84, 84, 92, 
110, 84, 58, 64, 60, 67, 65, 62, 68, 63, 65, 65, 74, 75, 75, 
100, 74, 80, 76, 116, 120, 110, 105, 88, 85, 88, 88, 88, 85, 
84, 90, 92, 74, 68, 68, 63, 70, 88, 75, 70, 67, 67, 67, 110, 
85, 92, 112, 96, 84, 90, 86, 52, 84, 79, 82), weight = c(3504, 
3693, 3436, 3433, 3449, 4341, 4354, 4312, 4425, 3850, 3563, 3609, 
3761, 3086, 2372, 2833, 2774, 2587, 2130, 1835, 2672, 2430, 2375, 
2234, 2648, 4615, 4376, 4382, 4732, 2130, 2264, 2228, 2634, 3439, 
3329, 3302, 3288, 4209, 4464, 4154, 4096, 4955, 4746, 5140, 2962, 
2408, 3282, 3139, 2220, 2123, 2074, 2065, 1773, 1613, 1834, 1955, 
2278, 2126, 2254, 2408, 2226, 4274, 4385, 4135, 4129, 3672, 4633, 
4502, 4456, 4422, 2330, 3892, 4098, 4294, 4077, 2933, 2511, 2979, 
2189, 2395, 2288, 2506, 2164, 2100, 4100, 3672, 3988, 4042, 3777, 
4952, 4464, 4363, 4237, 4735, 4951, 3821, 3121, 3278, 2945, 3021, 
2904, 1950, 4997, 4906, 4654, 4499, 2789, 2279, 2401, 2379, 2124, 
2310, 2472, 2265, 4082, 4278, 1867, 2158, 2582, 2868, 3399, 2660, 
2807, 3664, 3102, 2901, 3336, 1950, 2451, 1836, 2542, 3781, 3632, 
3613, 4141, 4699, 4457, 4638, 4257, 2219, 1963, 2300, 1649, 2003, 
2125, 2108, 2246, 2489, 2391, 2000, 3264, 3459, 3432, 3158, 4668, 
4440, 4498, 4657, 3907, 3897, 3730, 3785, 3039, 3221, 3169, 2171, 
2639, 2914, 2592, 2702, 2223, 2545, 2984, 1937, 3211, 2694, 2957, 
2945, 2671, 1795, 2464, 2220, 2572, 2255, 2202, 4215, 4190, 3962, 
4215, 3233, 3353, 3012, 3085, 2035, 2164, 1937, 1795, 3651, 3574, 
3645, 3193, 1825, 1990, 2155, 2565, 3150, 3940, 3270, 2930, 3820, 
4380, 4055, 3870, 3755, 2045, 2155, 1825, 2300, 1945, 3880, 4060, 
4140, 4295, 3520, 3425, 3630, 3525, 4220, 4165, 4325, 4335, 1940, 
2740, 2265, 2755, 2051, 2075, 1985, 2190, 2815, 2600, 2720, 1985, 
1800, 1985, 2070, 1800, 3365, 3735, 3570, 3535, 3155, 2965, 2720, 
3430, 3210, 3380, 3070, 3620, 3410, 3425, 3445, 3205, 4080, 2155, 
2560, 2300, 2230, 2515, 2745, 2855, 2405, 2830, 3140, 2795, 3410, 
1990, 2135, 3245, 2990, 2890, 3265, 3360, 3840, 3725, 3955, 3830, 
4360, 4054, 3605, 3940, 1925, 1975, 1915, 2670, 3530, 3900, 3190, 
3420, 2200, 2150, 2020, 2130, 2670, 2595, 2700, 2556, 2144, 1968, 
2120, 2019, 2678, 2870, 3003, 3381, 2188, 2711, 2542, 2434, 2265, 
2110, 2800, 2110, 2085, 2335, 2950, 3250, 1850, 2145, 1845, 2910, 
2420, 2500, 2290, 2490, 2635, 2620, 2725, 2385, 1755, 1875, 1760, 
2065, 1975, 2050, 1985, 2215, 2045, 2380, 2190, 2210, 2350, 2615, 
2635, 3230, 3160, 2900, 2930, 3415, 3725, 3060, 3465, 2605, 2640, 
2395, 2575, 2525, 2735, 2865, 1980, 2025, 1970, 2125, 2125, 2160, 
2205, 2245, 1965, 1965, 1995, 2945, 3015, 2585, 2835, 2665, 2370, 
2950, 2790, 2130, 2295, 2625, 2720), acceleration = c(12, 11.5, 
11, 12, 10.5, 10, 9, 8.5, 10, 8.5, 10, 8, 9.5, 10, 15, 15.5, 
15.5, 16, 14.5, 20.5, 17.5, 14.5, 17.5, 12.5, 15, 14, 15, 13.5, 
18.5, 14.5, 15.5, 14, 13, 15.5, 15.5, 15.5, 15.5, 12, 11.5, 13.5, 
13, 11.5, 12, 12, 13.5, 19, 15, 14.5, 14, 14, 19.5, 14.5, 19, 
18, 19, 20.5, 15.5, 17, 23.5, 19.5, 16.5, 12, 12, 13.5, 13, 11.5, 
11, 13.5, 13.5, 12.5, 13.5, 12.5, 14, 16, 14, 14.5, 18, 19.5, 
18, 16, 17, 14.5, 15, 16.5, 13, 11.5, 13, 14.5, 12.5, 11.5, 12, 
13, 14.5, 11, 11, 11, 16.5, 18, 16, 16.5, 16, 21, 14, 12.5, 13, 
12.5, 15, 19, 19.5, 16.5, 13.5, 18.5, 14, 15.5, 13, 9.5, 19.5, 
15.5, 14, 15.5, 11, 14, 13.5, 11, 16.5, 16, 17, 19, 16.5, 21, 
17, 17, 18, 16.5, 14, 14.5, 13.5, 16, 15.5, 16.5, 15.5, 14.5, 
16.5, 19, 14.5, 15.5, 14, 15, 15.5, 16, 16, 16, 21, 19.5, 11.5, 
14, 14.5, 13.5, 21, 18.5, 19, 19, 15, 13.5, 12, 16, 17, 16, 18.5, 
13.5, 16.5, 17, 14.5, 14, 17, 15, 17, 14.5, 13.5, 17.5, 15.5, 
16.9, 14.9, 17.7, 15.3, 13, 13, 13.9, 12.8, 15.4, 14.5, 17.6, 
17.6, 22.2, 22.1, 14.2, 17.4, 17.7, 21, 16.2, 17.8, 12.2, 17, 
16.4, 13.6, 15.7, 13.2, 21.9, 15.5, 16.7, 12.1, 12, 15, 14, 18.5, 
14.8, 18.6, 15.5, 16.8, 12.5, 19, 13.7, 14.9, 16.4, 16.9, 17.7, 
19, 11.1, 11.4, 12.2, 14.5, 14.5, 16, 18.2, 15.8, 17, 15.9, 16.4, 
14.1, 14.5, 12.8, 13.5, 21.5, 14.4, 19.4, 18.6, 16.4, 15.5, 13.2, 
12.8, 19.2, 18.2, 15.8, 15.4, 17.2, 17.2, 15.8, 16.7, 18.7, 15.1, 
13.2, 13.4, 11.2, 13.7, 16.5, 14.2, 14.7, 14.5, 14.8, 16.7, 17.6, 
14.9, 15.9, 13.6, 15.7, 15.8, 14.9, 16.6, 15.4, 18.2, 17.3, 18.2, 
16.6, 15.4, 13.4, 13.2, 15.2, 14.9, 14.3, 15, 13, 14, 15.2, 14.4, 
15, 20.1, 17.4, 24.8, 22.2, 13.2, 14.9, 19.2, 14.7, 16, 11.3, 
12.9, 13.2, 14.7, 18.8, 15.5, 16.4, 16.5, 18.1, 20.1, 18.7, 15.8, 
15.5, 17.5, 15, 15.2, 17.9, 14.4, 19.2, 21.7, 23.7, 19.9, 21.8, 
13.8, 18, 15.3, 11.4, 12.5, 15.1, 17, 15.7, 16.4, 14.4, 12.6, 
12.9, 16.9, 16.4, 16.1, 17.8, 19.4, 17.3, 16, 14.9, 16.2, 20.7, 
14.2, 14.4, 16.8, 14.8, 18.3, 20.4, 19.6, 12.6, 13.8, 15.8, 19, 
17.1, 16.6, 19.6, 18.6, 18, 16.2, 16, 18, 16.4, 15.3, 18.2, 17.6, 
14.7, 17.3, 14.5, 14.5, 16.9, 15, 15.7, 16.2, 16.4, 17, 14.5, 
14.7, 13.9, 13, 17.3, 15.6, 24.6, 11.6, 18.6, 19.4), year = c(70, 
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 
70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 71, 71, 71, 71, 
71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 
71, 71, 71, 71, 71, 71, 71, 72, 72, 72, 72, 72, 72, 72, 72, 72, 
72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 72, 
72, 72, 72, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 
73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 74, 74, 74, 74, 74, 
74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 
74, 74, 74, 74, 74, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 
75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 75, 
75, 75, 75, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 
76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 
76, 76, 76, 76, 76, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 
77, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 
78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 
78, 78, 78, 78, 78, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 
79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 
79, 79, 80, 80, 80, 80, 80, 80, 80, 80, 80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 1, 
1, 1, 1, 1, 1, 1, 3, 3, 1, 3, 1, 1, 3, 2, 2, 2, 2, 2, 3, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 1, 1, 2, 1, 2, 1, 1, 1, 
3, 2, 1, 1, 1, 1, 2, 3, 1, 3, 1, 1, 1, 1, 2, 3, 3, 3, 3, 3, 1, 
3, 2, 2, 2, 2, 3, 3, 2, 3, 3, 2, 3, 1, 1, 1, 1, 1, 3, 1, 3, 3, 
3, 3, 3, 1, 1, 1, 2, 3, 3, 3, 3, 2, 2, 3, 3, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 2, 3, 3, 1, 1, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 3, 
1, 1, 1, 2, 1, 1, 1), name = structure(c(49L, 36L, 231L, 14L, 
161L, 141L, 54L, 223L, 241L, 2L, 101L, 215L, 57L, 30L, 274L, 
220L, 8L, 150L, 95L, 282L, 211L, 16L, 258L, 22L, 7L, 133L, 68L, 
112L, 163L, 95L, 65L, 271L, 7L, 232L, 49L, 162L, 10L, 54L, 242L, 
141L, 223L, 116L, 129L, 250L, 9L, 64L, 243L, 151L, 187L, 208L, 
210L, 123L, 267L, 76L, 286L, 218L, 272L, 105L, 293L, 63L, 158L, 
54L, 241L, 223L, 141L, 3L, 192L, 31L, 203L, 75L, 183L, 11L, 48L, 
143L, 233L, 295L, 283L, 212L, 253L, 157L, 85L, 279L, 104L, 268L, 
25L, 10L, 55L, 142L, 109L, 193L, 44L, 148L, 222L, 74L, 29L, 1L, 
235L, 62L, 8L, 150L, 220L, 292L, 54L, 128L, 219L, 207L, 7L, 263L, 
63L, 87L, 174L, 156L, 188L, 121L, 59L, 244L, 124L, 209L, 17L, 
294L, 113L, 260L, 276L, 204L, 220L, 8L, 61L, 92L, 156L, 267L, 
63L, 50L, 10L, 234L, 142L, 27L, 110L, 143L, 11L, 21L, 284L, 209L, 
271L, 88L, 103L, 124L, 122L, 167L, 261L, 127L, 236L, 61L, 194L, 
150L, 241L, 42L, 224L, 148L, 24L, 41L, 10L, 221L, 34L, 60L, 154L, 
266L, 156L, 7L, 240L, 271L, 284L, 88L, 156L, 287L, 12L, 17L, 
211L, 296L, 260L, 171L, 125L, 208L, 40L, 103L, 254L, 50L, 108L, 
10L, 142L, 235L, 61L, 150L, 8L, 51L, 66L, 302L, 167L, 100L, 145L, 
252L, 13L, 287L, 91L, 266L, 156L, 297L, 239L, 211L, 276L, 185L, 
39L, 67L, 132L, 111L, 165L, 32L, 256L, 216L, 94L, 44L, 202L, 
117L, 189L, 53L, 35L, 238L, 144L, 245L, 58L, 70L, 160L, 288L, 
251L, 269L, 155L, 51L, 107L, 262L, 284L, 89L, 23L, 181L, 289L, 
139L, 180L, 93L, 171L, 201L, 114L, 195L, 249L, 55L, 135L, 136L, 
237L, 4L, 28L, 196L, 98L, 5L, 58L, 33L, 140L, 115L, 51L, 271L, 
84L, 118L, 265L, 230L, 206L, 77L, 19L, 298L, 259L, 214L, 291L, 
166L, 247L, 197L, 137L, 6L, 99L, 44L, 149L, 190L, 120L, 30L, 
129L, 56L, 73L, 304L, 173L, 106L, 15L, 186L, 38L, 211L, 201L, 
225L, 228L, 79L, 126L, 37L, 52L, 205L, 248L, 302L, 270L, 51L, 
82L, 52L, 134L, 4L, 98L, 18L, 273L, 175L, 86L, 266L, 176L, 103L, 
79L, 303L, 300L, 20L, 184L, 170L, 262L, 281L, 81L, 182L, 280L, 
164L, 229L, 35L, 97L, 52L, 229L, 277L, 217L, 169L, 261L, 80L, 
278L, 177L, 226L, 131L, 130L, 285L, 172L, 266L, 78L, 175L, 213L, 
299L, 275L, 90L, 24L, 200L, 146L, 72L, 45L, 47L, 46L, 246L, 96L, 
248L, 138L, 290L, 179L, 178L, 227L, 191L, 198L, 164L, 266L, 167L, 
168L, 83L, 26L, 199L, 71L, 147L, 264L, 102L, 43L, 153L, 301L, 
119L, 159L, 69L), .Label = c("amc ambassador brougham", "amc ambassador dpl", 
"amc ambassador sst", "amc concord", "amc concord d/l", "amc concord dl 6", 
"amc gremlin", "amc hornet", "amc hornet sportabout (sw)", "amc matador", 
"amc matador (sw)", "amc pacer", "amc pacer d/l", "amc rebel sst", 
"amc spirit dl", "audi 100 ls", "audi 100ls", "audi 4000", "audi 5000", 
"audi 5000s (diesel)", "audi fox", "bmw 2002", "bmw 320i", "buick century", 
"buick century 350", "buick century limited", "buick century luxus (sw)", 
"buick century special", "buick electra 225 custom", "buick estate wagon (sw)", 
"buick lesabre custom", "buick opel isuzu deluxe", "buick regal sport coupe (turbo)", 
"buick skyhawk", "buick skylark", "buick skylark 320", "buick skylark limited", 
"cadillac eldorado", "cadillac seville", "capri ii", "chevroelt chevelle malibu", 
"chevrolet bel air", "chevrolet camaro", "chevrolet caprice classic", 
"chevrolet cavalier", "chevrolet cavalier 2-door", "chevrolet cavalier wagon", 
"chevrolet chevelle concours (sw)", "chevrolet chevelle malibu", 
"chevrolet chevelle malibu classic", "chevrolet chevette", "chevrolet citation", 
"chevrolet concours", "chevrolet impala", "chevrolet malibu", 
"chevrolet malibu classic (sw)", "chevrolet monte carlo", "chevrolet monte carlo landau", 
"chevrolet monte carlo s", "chevrolet monza 2 2", "chevrolet nova", 
"chevrolet nova custom", "chevrolet vega", "chevrolet vega (sw)", 
"chevrolet vega 2300", "chevrolet woody", "chevy c10", "chevy c20", 
"chevy s-10", "chrysler cordoba", "chrysler lebaron medallion", 
"chrysler lebaron salon", "chrysler lebaron town @ country (sw)", 
"chrysler new yorker brougham", "chrysler newport royal", "datsun 1200", 
"datsun 200-sx", "datsun 200sx", "datsun 210", "datsun 210 mpg", 
"datsun 280-zx", "datsun 310", "datsun 310 gx", "datsun 510", 
"datsun 510 (sw)", "datsun 510 hatchback", "datsun 610", "datsun 710", 
"datsun 810", "datsun 810 maxima", "datsun b-210", "datsun b210", 
"datsun b210 gx", "datsun f-10 hatchback", "datsun pl510", "dodge aries se", 
"dodge aries wagon (sw)", "dodge aspen", "dodge aspen 6", "dodge aspen se", 
"dodge challenger se", "dodge charger 2.2", "dodge colt", "dodge colt (sw)", 
"dodge colt hardtop", "dodge colt hatchback custom", "dodge colt m/m", 
"dodge coronet brougham", "dodge coronet custom", "dodge coronet custom (sw)", 
"dodge d100", "dodge d200", "dodge dart custom", "dodge diplomat", 
"dodge magnum xe", "dodge monaco (sw)", "dodge monaco brougham", 
"dodge omni", "dodge rampage", "dodge st. regis", "fiat 124 sport coupe", 
"fiat 124 tc", "fiat 124b", "fiat 128", "fiat 131", "fiat strada custom", 
"fiat x1.9", "ford country", "ford country squire (sw)", "ford escort 2h", 
"ford escort 4w", "ford f108", "ford f250", "ford fairmont", 
"ford fairmont (auto)", "ford fairmont (man)", "ford fairmont 4", 
"ford fairmont futura", "ford fiesta", "ford futura", "ford galaxie 500", 
"ford gran torino", "ford gran torino (sw)", "ford granada", 
"ford granada ghia", "ford granada gl", "ford granada l", "ford ltd", 
"ford ltd landau", "ford maverick", "ford mustang", "ford mustang cobra", 
"ford mustang gl", "ford mustang ii", "ford mustang ii 2 2", 
"ford pinto", "ford pinto (sw)", "ford pinto runabout", "ford ranger", 
"ford thunderbird", "ford torino", "ford torino 500", "hi 1200d", 
"honda accord", "honda accord cvcc", "honda accord lx", "honda civic", 
"honda civic (auto)", "honda civic 1300", "honda civic 1500 gl", 
"honda civic cvcc", "honda prelude", "maxda glc deluxe", "maxda rx3", 
"mazda 626", "mazda glc", "mazda glc 4", "mazda glc custom", 
"mazda glc custom l", "mazda glc deluxe", "mazda rx-4", "mazda rx-7 gs", 
"mazda rx2 coupe", "mercedes-benz 240d", "mercedes-benz 280s", 
"mercedes benz 300d", "mercury capri 2000", "mercury capri v6", 
"mercury cougar brougham", "mercury grand marquis", "mercury lynx l", 
"mercury marquis", "mercury marquis brougham", "mercury monarch", 
"mercury monarch ghia", "mercury zephyr", "mercury zephyr 6", 
"nissan stanza xe", "oldsmobile cutlass ciera (diesel)", "oldsmobile cutlass ls", 
"oldsmobile cutlass salon brougham", "oldsmobile cutlass supreme", 
"oldsmobile delta 88 royale", "oldsmobile omega", "oldsmobile omega brougham", 
"oldsmobile starfire sx", "oldsmobile vista cruiser", "opel 1900", 
"opel manta", "peugeot 304", "peugeot 504", "peugeot 504 (sw)", 
"peugeot 505s turbo diesel", "peugeot 604sl", "plymouth 'cuda 340", 
"plymouth arrow gs", "plymouth champ", "plymouth cricket", "plymouth custom suburb", 
"plymouth duster", "plymouth fury", "plymouth fury gran sedan", 
"plymouth fury iii", "plymouth grand fury", "plymouth horizon", 
"plymouth horizon 4", "plymouth horizon miser", "plymouth horizon tc3", 
"plymouth reliant", "plymouth sapporo", "plymouth satellite", 
"plymouth satellite custom", "plymouth satellite custom (sw)", 
"plymouth satellite sebring", "plymouth valiant", "plymouth valiant custom", 
"plymouth volare", "plymouth volare custom", "plymouth volare premier v8", 
"pontiac astro", "pontiac catalina", "pontiac catalina brougham", 
"pontiac firebird", "pontiac grand prix", "pontiac grand prix lj", 
"pontiac j2000 se hatchback", "pontiac lemans v6", "pontiac phoenix", 
"pontiac phoenix lj", "pontiac safari (sw)", "pontiac sunbird coupe", 
"pontiac ventura sj", "renault 12 (sw)", "renault 12tl", "renault 18i", 
"renault 5 gtl", "renault lecar deluxe", "saab 99e", "saab 99gle", 
"saab 99le", "subaru", "subaru dl", "toyota carina", "toyota celica gt", 
"toyota celica gt liftback", "toyota corolla", "toyota corolla 1200", 
"toyota corolla 1600 (sw)", "toyota corolla liftback", "toyota corolla tercel", 
"toyota corona", "toyota corona hardtop", "toyota corona liftback", 
"toyota corona mark ii", "toyota cressida", "toyota mark ii", 
"toyota starlet", "toyota tercel", "toyouta corona mark ii (sw)", 
"triumph tr7 coupe", "vokswagen rabbit", "volkswagen 1131 deluxe sedan", 
"volkswagen 411 (sw)", "volkswagen dasher", "volkswagen jetta", 
"volkswagen model 111", "volkswagen rabbit", "volkswagen rabbit custom", 
"volkswagen rabbit custom diesel", "volkswagen rabbit l", "volkswagen scirocco", 
"volkswagen super beetle", "volkswagen type 3", "volvo 144ea", 
"volvo 145e (sw)", "volvo 244dl", "volvo 245", "volvo 264gl", 
"volvo diesel", "vw dasher (diesel)", "vw pickup", "vw rabbit", 
"vw rabbit c (diesel)", "vw rabbit custom"), class = "factor")), row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25", "26", "27", "28", "29", "30", "31", "32", "34", "35", "36", 
"37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", 
"48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", 
"59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", 
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", 
"81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", 
"92", "93", "94", "95", "96", "97", "98", "99", "100", "101", 
"102", "103", "104", "105", "106", "107", "108", "109", "110", 
"111", "112", "113", "114", "115", "116", "117", "118", "119", 
"120", "121", "122", "123", "124", "125", "126", "128", "129", 
"130", "131", "132", "133", "134", "135", "136", "137", "138", 
"139", "140", "141", "142", "143", "144", "145", "146", "147", 
"148", "149", "150", "151", "152", "153", "154", "155", "156", 
"157", "158", "159", "160", "161", "162", "163", "164", "165", 
"166", "167", "168", "169", "170", "171", "172", "173", "174", 
"175", "176", "177", "178", "179", "180", "181", "182", "183", 
"184", "185", "186", "187", "188", "189", "190", "191", "192", 
"193", "194", "195", "196", "197", "198", "199", "200", "201", 
"202", "203", "204", "205", "206", "207", "208", "209", "210", 
"211", "212", "213", "214", "215", "216", "217", "218", "219", 
"220", "221", "222", "223", "224", "225", "226", "227", "228", 
"229", "230", "231", "232", "233", "234", "235", "236", "237", 
"238", "239", "240", "241", "242", "243", "244", "245", "246", 
"247", "248", "249", "250", "251", "252", "253", "254", "255", 
"256", "257", "258", "259", "260", "261", "262", "263", "264", 
"265", "266", "267", "268", "269", "270", "271", "272", "273", 
"274", "275", "276", "277", "278", "279", "280", "281", "282", 
"283", "284", "285", "286", "287", "288", "289", "290", "291", 
"292", "293", "294", "295", "296", "297", "298", "299", "300", 
"301", "302", "303", "304", "305", "306", "307", "308", "309", 
"310", "311", "312", "313", "314", "315", "316", "317", "318", 
"319", "320", "321", "322", "323", "324", "325", "326", "327", 
"328", "329", "330", "332", "333", "334", "335", "336", "338", 
"339", "340", "341", "342", "343", "344", "345", "346", "347", 
"348", "349", "350", "351", "352", "353", "354", "356", "357", 
"358", "359", "360", "361", "362", "363", "364", "365", "366", 
"367", "368", "369", "370", "371", "372", "373", "374", "375", 
"376", "377", "378", "379", "380", "381", "382", "383", "384", 
"385", "386", "387", "388", "389", "390", "391", "392", "393", 
"394", "395", "396", "397"), class = "data.frame")

CodePudding user response:

rename works for me, perhaps you have a function conflict with another package. Try using dplyr::rename.

To change the columns based on range of values you may use case_when or cut.

library(dplyr)

Auto.df %>%
  dplyr::rename(mpgclass = mpg) %>%
  mutate(mpg = case_when(mpgclass < 20 ~'low', 
                         between(mpgclass, 20, 27) ~ 'medium', 
                         TRUE ~ 'high'), 
         mpg2 = cut(mpgclass, c(-Inf, 20, 27, Inf), c('low', 'medium', 'high')))

CodePudding user response:

You may use cut().

library(dplyr)

new <- mtcars %>%
  mutate(mpg_c=cut(mpg, c(0, 16, 22, Inf), labels=c('low', 'medium', 'hi')))

tail(new)
#                 mpg cyl disp  hp drat    wt  qsec vs am gear carb  mpg_c
# Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.7  0  1    5    2     hi
# Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.9  1  1    5    2     hi
# Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.5  0  1    5    4    low
# Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.5  0  1    5    6 medium
# Maserati Bora  15.0   8 301.0 335 3.54 3.570 14.6  0  1    5    8    low
# Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.6  1  1    4    2 medium

Base R:

mtcars |>
  transform(mpg_c=cut(mpg, c(0, 16, 22, Inf), labels=c('low', 'medium', 'hi')))
  • Related