After using the upper script, the Catcher4Test $ TimePlayed test converts 2250 to 2.25
Why?
Before script
After script
thanks
After using the upper script, the Catcher4Test $ TimePlayed test converts 2250 to 2.25
Why?
Before script
After script
thanks
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?
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