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!

+3


source to share


3 answers


Another approach (if you are reading this from a file):



dat <- read.table(text = "ID Distance
 M1_PRM    54,56
 M1_PRM  4147,69
 M1_PRM  1723,34",header = TRUE,sep = "",dec = ",")
> dat
      ID Distance
1 M1_PRM    54.56
2 M1_PRM  4147.69
3 M1_PRM  1723.34

      

+5


source


@joran's answer is the way to go if you read DB

with read.table

or read.csv

otherwise there type.convert

, which takes a parameter dec

.

type.convert(as.character(DB$Distance), dec = ",")
# [1]   54.56 4147.69 1723.34

      



Discard as.character

if Distance

already present.

+3


source


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

+1


source







All Articles