mpg cyl mpg1 mpg2
Mazda RX4 21.0 6 22.0 22.0
Mazda RX4 Wag 21.0 6 22.0 22.0
Datsun 710 22.8 4 23.8 23.8
Hornet 4 Drive 21.4 6 22.4 22.4
Hornet Sportabout 18.7 8 19.7 19.7
Valiant 18.1 6 19.1 19.1
data %>% mutate(mpg2=mpg+1,cyl2=if_else(cyl>6,'high','low'))
mpg cyl mpg1 mpg2 cyl2
Mazda RX4 21.0 6 22.0 22.0 low
Mazda RX4 Wag 21.0 6 22.0 22.0 low
Datsun 710 22.8 4 23.8 23.8 low
Hornet 4 Drive 21.4 6 22.4 22.4 low
Hornet Sportabout 18.7 8 19.7 19.7 high
Valiant 18.1 6 19.1 19.1 low
data %>% mutate(mpg2=mpg+1,cyl2=if_else(cyl>6,'high','low')) %>%
select(starts_with('cyl')) %>%
rownames_to_column(var='var') #将行名变成新列
var cyl cyl2
1 Mazda RX4 6 low
2 Mazda RX4 Wag 6 low
3 Datsun 710 4 low
4 Hornet 4 Drive 6 low
5 Hornet Sportabout 8 high
6 Valiant 6 low