Sunday 15 September 2013

Change decimal digits for data frame column in R -



Change decimal digits for data frame column in R -

questions displaying of numbers of digits have been posted, however, single values or vectors, hope can help me this.

i have info frame several columns , want display values in 1 column 2 decimal digits (this column only). have tried round() , format() , options(digits) none works on column (numerical). wonder if there method without going way of converting column vector , gluing again.

thanks lot!

here's illustration of how cars data.frame comes installed r.

first i'll add together variability have numbers decimal places:

data=cars+runif(nrow(cars))

then round single column (in case dist column 2 decimal places):

data[,'dist']=round(data[,'dist'],2)

if info contain whole numbers can guarantee values have 2 decimal places using:

cars[,'dist']=format(round(cars[,'dist'],2),nsmall=2)

r

No comments:

Post a Comment