0

I have a data:

df_1 <- data.frame(
  cpf = c('37576865879', '02177190948')
)

I try:

library(tidyverse)

str_sub(string = df_1$cpf, start = 4, end = 6) <- 'xxx'

#          cpf
#1 375xxx65879
#2 021xxx90948

The result is ok. But:

df_1 <- data.frame(
  cpf = c('37576865879', '02177190948')
)

df_1 %>% 
  mutate(.data = ., var = str_sub(string = cpf, start = 4, end = 6) <- "xxx")

#          cpf var
#1 37576865879 xxx
#2 02177190948 xxx

Only xs appears into var. How adjust this last function to get the complete vector?

#          cpf         var
#1 37576865879 375xxx65879
#2 02177190948 021xxx90948
neves
  • 796
  • 2
  • 10
  • 36

1 Answers1

1

To use this in pipes, you have to use str_sub<- method which can be used as :

library(dplyr)
library(stringr)

df_1 %>% mutate(var = `str_sub<-`(cpf, 4, 6, value = 'xxx'))

#         cpf         var
#1 37576865879 375xxx65879
#2 02177190948 021xxx90948
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213