0

After using the upper script, the Catcher4Test $ TimePlayed test converts 2250 to 2.25

Why?

Before script

Before script

After script

After script

thanks

Sathish
  • 12,453
  • 3
  • 41
  • 59
cf10061
  • 3
  • 2
  • Welcome to stackoverflow. Posting images of code is unhelpful as it means other users have to type your code out in order to help you. Please see [ask] to improve your question. – Simon.S.A. Apr 02 '20 at 21:12

2 Answers2

1

R interprets '.' as a decimal place. When you convert a string to a numeric, you get this error. For example:

as.numeric('2.050')
[1] 2.05

To prevent this, do:

> as.numeric(str_remove(string = Catcher4Test$TimePlayed, pattern = '\\.'))
[1] 2250

Sidenote: Be careful, I would strongly suggest to rather use the below solution to be 100% sure that transformation of your character variables is correct:

> as.numeric(str_remove(string = as.character(Catcher4Test$TimePlayed), pattern = '\\.'))
[1] 2250

This is due to below problem: What's wrong with as.numeric in R?

Jan Janiszewski
  • 432
  • 3
  • 14
0

It looks like your data uses "." to separate hundredths from thousandths. I would use readr::parse_number to tell R how to interpret your data. This is an example:

readr::parse_number("2.250", locale = locale(grouping_mark = "."))

I would suggest using one of the readr functions (read_csv, read_tsv etc.) to read in your data cleanly so you don't have to munge it after the fact.

readr::read_*("file", locale = locale(grouping_mark = "."))

There is a section on this in the excellent and free R for Data Science resource

Conor
  • 131
  • 5