Regex in r to replace string without special characters

I do my regular expression with r on the football schedule and can't figure it out.

I essentially try to change any home game to the HOME line. here is a snippet of the sched_team graphic block that I am using:

  Team   w1   w2   w3   w4   w5   w6   w7   w8   w9  w10  w11  w12  w13  w14
1  ARI   SD @NYG   SF  BYE @DEN  WSH @OAK  PHI @DAL  STL  DET @SEA @ATL   KC
2  ATL   NO @CIN   TB @MIN @NYG  CHI @BAL  DET  BYE  @TB @CAR  CLE  ARI  @GB
3  BAL  CIN  PIT @CLE  CAR @IND  @TB  ATL @CIN @PIT  TEN  BYE  @NO   SD @MIA

non-home commands have an @ character to start a line. home teams do not. using regex in python. I believe that all home teams can be selected with a regular expression, for example: ^ ([AZ]) \ w + .. essentially it starts with capital. this does not work in R due to \ w among other errors.

Here is what I tried (and failed):

str_replace_all(as.matrix(schedule_teams), "[[^([A-Z])\w+]]", "HOME")

Is there an easier way to change all home teams to HOMES?

early

+4
source share
1

, , , , )

, (, , ), .

[[^([A-Z]  # any character of: '[', '^', '(', '[', 'A' to 'Z' 

, , , , escape \w , .

, .

> df[,-1] <- str_replace_all(as.matrix(df[,-1]), '^[A-Z]\\w+', 'HOME')
##   Team   w1   w2   w3   w4   w5   w6   w7   w8   w9  w10  w11  w12  w13  w14
## 1  ARI HOME @NYG HOME HOME @DEN HOME @OAK HOME @DAL HOME HOME @SEA @ATL HOME
## 2  ATL HOME @CIN HOME @MIN @NYG HOME @BAL HOME HOME  @TB @CAR HOME HOME  @GB
## 3  BAL HOME HOME @CLE HOME @IND  @TB HOME @CIN @PIT HOME HOME  @NO HOME @MIA

stringr, , sub, .

> df[,-1] <- sub('^[A-Z]\\w+', 'HOME', as.matrix(df[,-1]))

:

> m <- as.matrix(df[-1])
> m[substr(m,0,1) != '@'] <- 'HOME'
> cbind(df[1], m)
##   Team   w1   w2   w3   w4   w5   w6   w7   w8   w9  w10  w11  w12  w13  w14
## 1  ARI HOME @NYG HOME HOME @DEN HOME @OAK HOME @DAL HOME HOME @SEA @ATL HOME
## 2  ATL HOME @CIN HOME @MIN @NYG HOME @BAL HOME HOME  @TB @CAR HOME HOME  @GB
## 3  BAL HOME HOME @CLE HOME @IND  @TB HOME @CIN @PIT HOME HOME  @NO HOME @MIA
+5

All Articles