Karsen Mitsche
Karsen Mitsche

Reputation: 57

How can I get this gsub function to work for me?

Very new to R, and am learning a lot on the fly by experimenting with building a model. I have tried searching for the solution but have not found anything.

I have a dataframe named HPPH that looks like this. I am trying to reformat it to a number. Ex: 39' 11" changed to 39.11

      player_name      PH        
  <chr>            <chr>     
1 Chad Campbell    "39' 11\""
2 Charl Schwartzel "39' 7\"" 
3 Ángel Cabrera    "39' 6\"" 
4 Vaughn Taylor    "39' 4\"" 
5 Stephen Ames     "39' 4\"" 

When I use gsub("' ", ".", HPPH$PH) I get this result:

> gsub("' ", ".", HPPH$PH)
 [1] "39.11\"" "39.7\""  "39.6\""  "39.4\""  "39.4\""  "39.1\""  "39.1\""  "39.0\""  "38.10\"" "38.4\""  "40.7\"" 
[12] "38.2\""  "38.1\""  "38.1\""  "37.7\""  "37.4\""  "37.2\""  "37.1\""  "37.0\""  "36.11\"" "36.9\""  "36.8\"" 
[23] "38.2\""  "36.5\""  "40.9\""  "40.10\"" "48.11\"" "40.10\"" "47.3\""  "45.11\"" "45.10\"" "45.7\""  "45.5\"" 
[34] "44.7\""  "43.8\""  "43.1\""  "42.10\"" "41.6\""  "40.10\"" "46.3\""  "36.4\""  "36.2\""  "36.0\""  "29.8\"" 
[45] "29.10\"" "29.10\"" "35.10\"" "35.9\""  "35.7\""  "35.6\""  "35.5\""  "35.5\""  "35.2\""  "35.2\""  "35.0\"" 
[56] "34.8\""  "34.8\""  "34.4\""  "34.0\""  "33.9\""  "33.6\""  "33.5\""  "33.1\""  "33.0\""  "31.10\"" "31.5\"" 
[67] "31.3\""  "31.0\""  "30.11\""

So it seems as if it's doing what it is supposed to but when I view HPPH, it is not changing anything.

> head(HPPH)
# A tibble: 6 x 2
  player_name      PH        
  <chr>            <chr>     
1 Chad Campbell    "39' 11\""
2 Charl Schwartzel "39' 7\"" 
3 Ángel Cabrera    "39' 6\"" 
4 Vaughn Taylor    "39' 4\"" 
5 Stephen Ames     "39' 4\"" 
6 David Toms       "39' 1\"" 

If I assign it with HPPH$PH <- gsub("' ", ".", HPPH$PH)it gets turned into a value and when I use View(HPPH) it now shows up as this.

HPPH

Upvotes: 2

Views: 59

Answers (1)

akrun
akrun

Reputation: 887028

We need to assign it to the object

HPPH$PH <- gsub("' ", ".", HPPH$PH)

Upvotes: 2

Related Questions