Removing ★ symbol from a column?

50 views Asked by At

Attempting to remove ★ symbol from a column to no avail.

dput(star$fifa21_raw_data.W.F[1:20])

c("4 ★", "4 ★", "3 ★", "5 ★", "5 ★", "4 ★", "4 ★", "3 ★", "3 ★", "4 ★", "3 ★", "4 ★", "3 ★", "3 ★", "4 ★", "4 ★", "3 ★", "4 ★", "3 ★", "4 ★")

I looked everywhere and feel as though gsub will do the trick here so I tried:

gsub('[★]([0-9]+)','\\1\\2', star$fifa21_raw_data.W.F)

and R printed the same result:

[1] "4 ★" "4 ★" "3 ★" "5 ★" "5 ★" "4 ★" "4 ★" "3 ★" "3 ★" "4 ★" [11] "3 ★" "4 ★" "3 ★" "3 ★" "4 ★" "4 ★" "3 ★" "4 ★" "3 ★" "4 ★"

I am at a loss. Thank you for assisting me in this.

2

There are 2 answers

1
The fourth bird On BEST ANSWER

Besides that your pattern [★]([0-9]+) does not match the example data, note that there is just a single capture group for the digits so there is no \\2.

If you want to match the format in your example data, you can start by capturing a single digit and match the spaces after it:

([0-9])\\s+★

Which will match:

  • ([0-9]) Capture a single digit in group 1
  • \\s+ Match 1 or more whitespace characters (or just spaces if you want)
  • Match literally

Use group 1 in the replacement.

items <- c("4 ★", "4 ★", "3 ★", "5 ★", "5 ★", "4 ★", "4 ★", "3 ★", "3 ★", "4 ★", "3 ★", "4 ★", "3 ★", "3 ★", "4 ★", "4 ★", "3 ★", "4 ★", "3 ★", "4 ★")
result <- gsub("([0-9])\\s+★", "\\1", items)

print(result)

Output

[1] "4" "4" "3" "5" "5" "4" "4" "3" "3" "4" "3" "4" "3" "3" "4" "4" "3" "4" "3" "4"
1
Alex J On

Your regular expression is not matching your data. It is looking for a star followed by numbers, but your input data is a number followed by a space and a star. If instead we replace the space and star with an empty string:

gsub(" ★","",star$fifa21_raw_data.W.F)

It returns the following

 [1] "4" "4" "3" "5" "5" "4" "4" "3" "3" "4" "3" "4" "3" "3" "4" "4" "3" "4" "3" "4"

If you want to then convert from a vector of characters, you could then use the as.numeric() function.