I want to extract only the word landfill from columns x1 and x3 and place them into a new column.
I first extracted then I tried to place it in a new column but it did not work.
Below are a couple ways to get to your desired outcome. The first modifies your code by removing B$ in the substr() function (not needed because of the pipe %>%), as well as adjusting the start and end arguments to 7 and 14. The second uses the str_extract() function.
library(tidyverse)
B <- tibble(x = 1:4,
x2 = c("2", "1", "1", "block.landfill"),
x3 = c("block.landfill", "1", "2", "tu"))
# adjusting your example
tibble(x = 1:4,
x2 = c("2", "1", "1", "block.landfill"),
x3 = c("block.landfill", "1", "2", "tu")
) %>%
mutate(shift1 = substr(x2, 7, 14),
new = substr(x3, 7, 14))
#> # A tibble: 4 × 5
#> x x2 x3 shift1 new
#> <int> <chr> <chr> <chr> <chr>
#> 1 1 2 block.landfill "" "landfill"
#> 2 2 1 1 "" ""
#> 3 3 1 2 "" ""
#> 4 4 block.landfill tu "landfill" ""
# using the str_extract function
B %>%
mutate(shift1 = str_extract(x2, 'landfill'),
new = str_extract(x3, 'landfill'))
#> # A tibble: 4 × 5
#> x x2 x3 shift1 new
#> <int> <chr> <chr> <chr> <chr>
#> 1 1 2 block.landfill <NA> landfill
#> 2 2 1 1 <NA> <NA>
#> 3 3 1 2 <NA> <NA>
#> 4 4 block.landfill tu landfill <NA>