0

I have a data frame which has NA's in the data. I can easily reformat a whole column to 3 decimal places disregarding the NA's. However, I can't find a way to do the entire dataframe. is there no easier way to do this than separately operating on the columns? I didn't have any luck getting apply to work either.

> ndf3
                    a                  b                 c                   d                  e
1   0.144346473029046 0.0945535269709544 0.127334024896266   0.125881659751037  0.108239419087137
2              0.1376             0.1489            0.0949             0.11784             0.1372
3    1.48794219374094   1.53595988141984  1.21977361926472    1.15852039195536  0.707774430272806
4    3.96992500677986   3.90772031563767  3.61382453467775    3.39967072835538   4.30292753254034
5   0.174889714039075 -0.419130501555829 0.113585656836338 -0.0307388258550179 -0.309314899645693
6   0.135682205983122  0.189426931757576 0.158335341568249   0.116675569765867 0.0645715900731325
252  37.8736196377026   22.0640317131964  33.4935080490794    33.2572127571306   29.0085133196937
8   0.335796353933727  0.179593968145971 0.484609677920302                  NA                 NA
9   0.788381742738589  0.742738589211618 0.767634854771784   0.813278008298755                  1
10   1.42703970549072   1.33470191247724  1.25827986596585    1.32419222712559                  1

> class(ndf3)
[1] "data.frame"
> typeof(ndf3)
[1] "list"
> c <- format(round(as.numeric(as.character(ndf3)),3), nsmall = 3, na.encode = TRUE)
Warning message:
In format(round(as.numeric(as.character(ndf3)), 3), nsmall = 3,  :
  NAs introduced by coercion
> c
[1] "NA" "NA" "NA" "NA" "NA"


> is.character(ndf3)
[1] FALSE
> is.character(ndf3[[1]])
[1] TRUE
> round(as.numeric(ndf3[[1]]),3)
 [1]  0.144  0.138  1.488  3.970  0.175  0.136 37.874  0.336  0.788  1.427

2 Answers 2

1

If you just want to round to 3 digits:

ndf3 <- data.frame(lapply(ndf3, round, 3))

Edit: the above method lets you apply any operation to a whole data.frame. E.g., to coerce to numeric and then round to 3 digits:

lapply(ndf3, function(x) round(as.numeric(x), 3))

Or if you prefer not to use an anonymous function, you can define the function in advance. This is equivalent to the above:

forced_numeric_round3 <- function(x) round(as.numeric(x), 3)
lapply(ndf3, forced_numeric_round3)
2
  • This doesn't work because the items are non-numeric. above you can see I tried to coerce them but I get an unexpected result. I just want to know if it is possible to coerce the whole data.frame at once and round all non NAs and leave NAs in tact. Seems like there should be a really simple task but it is proving otherwise, for me at least Nov 4, 2014 at 15:44
  • What I showed above lets you apply any operation to a whole data.frame. Have you tried running with it from there? e.g. lapply(ndf3, function(x) round(as.numeric(x), 3))
    – arvi1000
    Nov 4, 2014 at 20:32
-1

If every column in the data frame is numeric: as.data.frame(round(as.matrix(ndf3),3))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.