简体   繁体   中英

How to rename a string within a column that contains a backslash in R

I have a million row dataset, df,

  Name         

  \\tia@gmail.com\Out
  \\tia@gmail.com\Out
  \\tia@gmail.com\Out
  \\tia@gmail.com\In
  \\tia@gmail.com\In
  \\tia@gmail.com\In

I would like to replace all instances where \\tia@gmail.com\\Out occurs to Outboxdata. This is my desired output:

  Name

  Outboxdata
  Outboxdata
  Outboxdata
  \\tia@gmail.com\In
  \\tia@gmail.com\In
  \\tia@gmail.com\In

Here is the dput:

 structure(list(Name = structure(c(2L, 2L, 2L, 1L, 1L, 1L), .Label =       c("\\\\tia@gmail.com\\In", 
 "\\\\tia@gmail.com\\Out"), class = "factor")), class = "data.frame", row.names   = c(NA, 
 -6L))

This is what I am starting with, I know I can use dplyr and gsub:

df %>%
   gsub,df$Name

I do not have the correct syntax, and I will continue to research this

We can use str_replace

library(dplyr)
library(stringr)
df %>%
  mutate(Name = str_replace(Name, ".*[^A-Za-z]([A-Za-z]+)$", "\\1boxdata"))
#        Name
#1 Outboxdata
#2 Outboxdata
#3 Outboxdata
#4 Outboxdata

Update

If we want to replace only on strings having tia@gmail.com, then

df %>%
   mutate(Name = str_replace(Name, ".*tia@gmail.com\\\\+Out", "Outboxdata"))

Note: As we are replacing values in a particular column, use the function within mutate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM