2

I am working with the following character vector of fractions:

dput(my_vec)
c("85½", "75", "73", "91½", "93½", "68½", "74", "94½", "81½", 
"67½", "96½", "71", "84½", "96½", "64½", "84½", "82½", 
"81½", "94½", "74½", "76½", "73", "69½", "83", "81½", "85½", 
"76", "77", "81", "92½")

and I need to convert this vector into an actual numeric vector with the values. Unfortunately, simply doing the following doesn't work:

> as.numeric(my_vec)
[1] NA 75 73 NA NA NA 74 NA NA NA NA 71 NA NA NA NA NA NA NA NA NA 73 NA 83 NA NA 76 77 81 NA
Warning message:
NAs introduced by coercion 

Has anyone seen this before and would know how to handle getting these special character fractions into decimals in R? Thanks!

Canovice
  • 9,012
  • 22
  • 93
  • 211
  • 1
    There are only a relatively small number of these unicode fraction characters - http://unicodefractions.com/ - so you can just do a find-and-replace of the set 1-by-1 if need be. – thelatemail Feb 27 '18 at 22:13

1 Answers1

3

You can use sub to change the special character to .5

as.numeric(sub('½', ".5", my_vec))
 [1] 85.5 75.0 73.0 91.5 93.5 68.5 74.0 94.5 81.5 67.5 96.5 71.0 84.5 96.5 64.5
[16] 84.5 82.5 81.5 94.5 74.5 76.5 73.0 69.5 83.0 81.5 85.5 76.0 77.0 81.0 92.5
G5W
  • 36,531
  • 10
  • 47
  • 80