How to round all values in a matrix?

40,778

Just as in your other question, use the function (round) you found :)

corrs <- round(cor(dataset, use="pairwise.complete.obs"), 2)

For example:

> round(cor(cars),2)
      speed dist
speed  1.00 0.81
dist   0.81 1.00
Share:
40,778

Related videos on Youtube

Tim Cooper
Author by

Tim Cooper

Are you 100% sure that you will go to Heaven when you die? Watch this video!

Updated on July 09, 2022

Comments

  • Tim Cooper
    Tim Cooper almost 2 years

    I use the following method to store all my correlations in a matrix:

    corrs <- cor(dataset, use="pairwise.complete.obs")
    

    But now I'd like to round the values to two digits after the comma. How can I do that? I just found a round function but don't know how to apply it to all values.

  • daroczig
    daroczig about 13 years
    @Roflcoptr: you are very welcome! It takes some time to get used to the wonderful world of R, I also find interesting (but trivial for others) function every day :) And learning the lang definitely worths it! If you feel lost and have time, check the docs about R which would provide a quite structured knowledge (what I lack unfortunately): cran.r-project.org/manuals.html, and also check "R inferno" which is a great resource!

Related