I am trying to replace values found in one column of a dataframe based upon finding a match in another dataframe using mutate(). Here is an example:
rename_ds <- data.frame(
car_name = c("Camaro Z28","AMC Javelin"),
replace_with = c("Camaro","Javelin"),
stringsAsFactors = FALSE)
mt_cars <- mtcars %>%
tibble::rownames_to_column() %>%
dplyr::rename("car_name" = rowname) %>%
dplyr::mutate(car_name = ifelse(car_name %in% rename_ds$car_name,
rename_ds[which(rename_ds$car_name == car_name),2],
car_name)
When I run this, instead of the car names being replaced by their respective replacements in rename_ds$replace_with, they are NA.
21 Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
22 Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
23 <NA> 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
24 <NA> 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
25 Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
26 Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
27 Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Any suggestions? Thanks in advance.