-1

I am new to R and I have a dataframe that has a character class column with the following format:

> head(unique(season_advanced_stats$Player))
[1] "alex abrines\\abrinal01"  "quincy acy\\acyqu01"      "steven adams\\adamsst01" 
[4] "arron afflalo\\afflaar01" "alexis ajinca\\ajincal01" "cole aldrich\\aldrico01" 

I was trying to use gsub but Rstudio kept outputing the "+" symbol everytime I entered the following code indicating that I haven't finished something:

season_advanced_stats$Player = gsub('\', '', season_advanced_stats$Player)

If anyone can help me figure out how to properly use gsub so only players names are remaining I'd appreciate it!

Keenan Burke-Pitts
  • 475
  • 2
  • 6
  • 17
  • You want to remove also numbers? If not, try `gsub("\\\\", "",season_advanced_stats$Player)` – Miha Jul 10 '17 at 17:52
  • I want to remove "\\" and the abbreviated name with numbers in them that comes after the backslashes. – Keenan Burke-Pitts Jul 10 '17 at 17:56
  • 1
    Then try this: `gsub("\\\\|\\d", "",season_advanced_stats$Player)` – Miha Jul 10 '17 at 17:56
  • This is close but it still outputs the characters after the backslashes. How do you recommend I remove everything after the backslashes as well so only the players names before the backslashes remain? – Keenan Burke-Pitts Jul 10 '17 at 18:02

2 Answers2

3

You can remove everything after backslash with using

 gsub("\\\\.*", "", data/vector)

So my example

v <- c("alex abrines\\abrinal01" , "quincy acy\\acyqu01") 

gsub("\\\\.*", "", v)

And output

[1] "alex abrines" "quincy acy" 
Miha
  • 2,559
  • 2
  • 19
  • 34
1

You need to escape the /. Currently, R thinks '\', ', after gsub, is a single string, because the / is actually escaping the first ', making R ignore it as far as ending that first string goes. It's giving you a + as a prompt for the final ', which it believes it is still waiting for.

season_advanced_stats$Player = gsub('\\', '', season_advanced_stats$Player)

Should work.

See this question.

BLT
  • 2,492
  • 1
  • 24
  • 33