There are a number of ways to accomplish data formatting in R.
Data Options in R
R supports a range of data formats and controls. The options() function accesses the default settings R establishes at start-up. Session options that can be changed from the command line include:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
> names(options()) [1] "add.smooth" "bitmapType" "browser" [4] "browserNLdisabled" "check.bounds" "continue" [7] "contrasts" "defaultPackages" "demo.ask" [10] "device" "device.ask.default" "digits" [13] "dvipscmd" "echo" "editor" [16] "encoding" "example.ask" "expressions" [19] "help_type" "help.search.types" "help.try.all.packages" [22] "HTTPUserAgent" "internet.info" "keep.source" [25] "keep.source.pkgs" "locatorBell" "mailer" [28] "max.print" "menu.graphics" "na.action" [31] "nwarnings" "OutDec" "pager" [34] "papersize" "pdfviewer" "pkgType" [37] "printcmd" "prompt" "repos" [40] "rl_word_breaks" "scipen" "show.coef.Pvalues" [43] "show.error.messages" "show.signif.stars" "str" [46] "str.dendrogram.last" "stringsAsFactors" "texi2dvi" [49] "timeout" "ts.eps" "ts.S.compat" [52] "unzip" "useFancyQuotes" "verbose" [55] "warn" "warning.length" "width" |
Each of these variables can be changed to modify R performance. For more details on each element see the HTML help for the options() function. A practical example is given below.
Data Formats in R
The options() function is frequently used to set the digit setting. In practice, the format change does not impact the data object, its’ content or precision, just data object’s appearance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Manage number of digits > options(digits=4) > pi [1] 3.142 > options(digits=16) > pi [1] 3.141592653589793 # Manage scientific notation > options(digits=5) > print(1e5) [1] 1e+05 > options(scipen=3); > print(1e5) [1] 100000 |
Meanwhile, the format() function can used to achieve pretty printing:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
> temp <- data.frame(col1 = c(111111, 222222, 333333), col2 = c(999999, 888888, 777777)) > temp col1 col2 1 111111 999999 2 222222 888888 3 333333 777777 > format(temp, scientific = FALSE, big.mark = ",") col1 col2 1 111,111 999,999 2 222,222 888,888 3 333,333 777,777 |
Data Object Editing
The best way to edit data objects is in R directly. However, R data objects can also be edited by invoking a text editor with the fix() and edit() functions. Both functions launch the text editor as specified by the editor argument in the option() function. Once the editor is open, a window similar to a crude spreadsheet appears, and data object elements can be changed manually. All changes are then saved back to the .Data folder when the editor is closed.