Why does R drop the decimal when as.numeric is applied?
Hi all the community, I have the following DB:
ID Distance
M1_PRM 54,56
M1_PRM 4147,69
M1_PRM 1723,34
I am using the following script to replace "," with ".". in Distance, since R doesn't like "," (and it works):
mysub<-function(x)(sub(",",".",x))
DB<-(apply(DB, 2,mysub))
DB<-data.frame(DB)
Then I need to convert DB $ Distance as.numeric
, because I need to use
tapply
in combination with sum, for example:
apply(DB$Distance,ID,sum)
When i give
DB$Distance<-as.numeric(DB$Distance)
ID Distance
M1_PRM 54
M1_PRM 4147
M1_PRM 1723
It seems like R is dropping the decimal !!! Does anyone know what is wrong? Thanks in advance!
source to share
R is discarding the decimal because you are wrong in the call apply
, try instead
> DB$Distance <- as.numeric(sub(",",".",DB$Distance))
> sapply(DB, class)
ID Distance
"factor" "numeric"
> DB
ID Distance
1 M1_PRM 54.56
2 M1_PRM 4147.69
3 M1_PRM 1723.34
Then use tapply
as in:
with(DB, tapply(Distance, ID, sum))
yours apply(DB$Distance,ID,sum)
won't work, use instead tapply(DB$Distance, DB$ID, sum)
, because the function is correct tapply
, and you need to give a numeric pointer and an index, both of them are attached in DB
, so R won't find ID
if you don't use the function with(.)
or DB$ID
.
see ?apply
and ?tapply
.
I am just trying to give you an answer as per your post. @ Joran's answer is a straight forward way to go if you are importing data from a file, if so all your problems boil down to using dec = ","
in a callread.table
source to share