简体   繁体   中英

Using dplyr to gather specific dummy variables

This question is the extension of ( Using dplyr to gather dummy variables ) .

The question: How can I gather only a few columns , instead of the whole dataset? So in this example, I want to gather all the columns, but except "sedan". My real data set has 250 columns, so therefore it will be great if I can include/exclude the columns by name .

Data set

head(type)
x    convertible coupe hatchback sedan wagon
1           0     0         0     1     0
2           0     1         0     0     0
3           1     0         0     0     0
4           1     0         0     0     0
5           1     0         0     0     0
6           1     0         0     0     0

Output

TypeOfCar
1     x
2     coupe 
3     convertible
4     convertible
5     convertible
6     convertible

Not sure if i'm understanding you, but you can do what you want:

df %>% select(-sedan) %>%  gather(Key, Value)

And if you have to much variables you can use:

select(-contains(""))
select(-start_wi(""))
select(-ends_with(""))

Hope it helps.

You can use -sedan in gather :

dat %>% gather(TypeOfCar, Count, -sedan) %>% filter(Count >= 1) %>% select(TypeOfCar)
#      TypeOfCar
# 1 convertible
# 2 convertible
# 3 convertible
# 4 convertible
# 5       coupe

Data:

tt <- "convertible coupe hatchback sedan wagon
1           0     0         0     1     0
2           0     1         0     0     0
3           1     0         0     0     0
4           1     0         0     0     0
5           1     0         0     0     0
6           1     0         0     0     0"

dat <- read.table(text = tt, header = T)

Fixed it with a combination of @RLave and @Carlos Vecina

right_columns <- all_data %>% select(starts_with("hour"))

all_data$all_hour <-data.frame(new_column = names(right_columns )[as.matrix(right_columns )%*%seq_along(right_columns )],stringsAsFactors=FALSE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM