R: replacing a repeating variable's name in a column

Viewed 45

I was hoping someone could help me because this problem should be really easy to solve, however it's taking me too much time.

I have a df (data) with several columns. In one of the columns (rideable_type) I get one of 3 answers, "docked_bike" "classic_bike" "electric_bike".

I want to turn every "docked_bike" answer into "classic_bike".

When I use

data$rt<-data$rideable_type %>%
  set_names(~stringr::str_replace_all(.,"docked_bike", "classic_bike"))

Nothing changes.

When I use

data$rt<-data$rideable_type %>% 
  dplyr::rename_all(~stringr::str_replace_all(.,"docked_bike", "classic_bike"))

I get an error:

Error in UseMethod("tbl_vars") :
no applicable method for 'tbl_vars' applied to an object of class "character"

Thank you for your time

1 Answers

I found the answer, maybe the question wasn't clear. So the point was to change the values within the column. But maybe that is not correct R terminology

hopefully it'll help someone else.

data$rt<-NA
data$rt[data$rideable_type=="docked_bike"]<-"classic_bike"
data$rt[data$rideable_type=="classic_bike"]<-"classic_bike"
data$rt[data$rideable_type=="electric_bike"]<-"electric_bike"
data=subset(data, select = -c(rt) )
Related