Reputation: 2081
I want to replace those NA
on column y
by the vector subst <- c(5,6,7)
. My result is out of order and also I think this is no the right way of doing what I want.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df <- tibble(x = c("a", "a", "b", "b", "b", "c", "c"),
y = c(2, 3, NA, NA, NA, 1, 2))
subst <- c(5, 6, 7)
df2 <- df %>% mutate(y = ifelse(x == "b", subst, y))
# But I want to obtain
df3 <- tibble(x = c("a", "a", "b", "b", "b", "c", "c"),
y = c(2, 3, 5, 6, 7, 1, 2))
Created on 2021-06-07 by the reprex package (v2.0.0)
Upvotes: 2
Views: 22