
Word Cloud Links
Animation (3) CSP (2) Data (22) Data Science (3) Distributions (1) Dust (7) Economics (14) Engineering (9) Equipment Venders (1) Faster R (1) GDAL (5) Germany (1) ggplot2 (19) GIS (5) Irradiance (17) Kuwait (1) LaTeX (11) Linux (2) Meteorology (16) Misc Tricks (3) Modeling (6) Natural Gas (2) Nuclear (1) O&M (2) Projects (5) Project Valuations (6) Qatar RE (13) R Colors (9) R Data Import (6) R Data Objects (16) R Data Syntax (6) Renewable Energy (14) RE Policy (7) Resource Assessment (19) R Graphics (19) R Packages (3) R Programming (20) Saudi Arabia (2) Scientific Computing (1) Solar (35) Spatial Analysis (6) Storage (1) UAE (3) Ubuntu (1) Website (2) WECC (2) Wind (5)Rbloggers
Renewable Energy World
Archives
Category Archives: R Data Objects
Data Modes and Classes in R
In R, data modes and classes define the fundamental attributes and behavior of a data object. For example, different modes and classes are handled differently by core functions like print(), summary(), and plot().
Data Object Modes
All data in R is an object and all objects have a “mode.” The mode determines what type of information can be found within the object and how that information is stored. Atomic “modes” are the basic building blocks for data objects in R. There are 6 basic atomic modes:
Posted in R Data Objects
Leave a comment
Data Object Management
Data Object Management
The following functions are useful for data object management in R:
Function  Description 

class()  Identify the class of a named object. 
colnames(); rownames()  Retrieve or set the column or row names of an object. 
dim()  Retrieve or set the dimensions of a rectangular data object. 
dimnames()  Get or set the dim names of an object. 
head()  Returns the first n rows of a data object. 
Posted in R Data Objects
Leave a comment
Data Sequences and Repetition in R
Data sequences and repetition are useful functions to define data objects, create new objects, control extractions or replacement, and manage function routines.
Data Sequences
The seq() function can be used several ways depending on its argument structure:
1 2 3 4 5 6 
seq(from, to) seq(from, to, by= ) seq(from, to, length.out= ) seq(along.with= ) seq(from) seq(length.out= ) 
The first form generates the sequence from a number to a number and is identical to from:to:
1 2 3 4 
> seq(3, 3) [1] 3 2 1 0 1 2 3 > 3:3 [1] 3 2 1 0 1 2 3 
The second form generates a sequence from:to with the step length by:
Posted in R Data Objects
Leave a comment
Data Sorting in R
Data sorting in R is simple and straightforward. Key functions include sort() and order(). The variable by which sort you can be a numeric, a string or a factor variable. Argument options also provide flexibility how missing values will be handled: they can be listed first, last or removed.
Data Sorting Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
> x < c(0.868, 0.066, 0.075, 1.002, 0.646) > sort(x) [1] 1.002 0.075 0.066 0.646 0.868 > order(x) [1] 4 3 2 5 1 > x[order(x)] [1] 1.00203069 0.07577924 0.06647998 0.64641650 0.86889398 > x < rep(1:4, each = 2) > x [1] 1 1 2 2 3 3 4 4 > unique(x) [1] 1 2 3 4 > rev(unique(x)) [1] 4 3 2 1 
It is also possible to sort in reverse order by using a minus sign ( – ) in front of the sort variable. For example:
1 2 
> sort(x) [1] 0.868 0.646 0.066 0.075 1.002 
Posted in R Data Objects, R Programming
Leave a comment
R Data Subscripting
Intro to R Data Subscripting
R data subscripting is a key “motor skill” to extract data by row, column or element. Subscripting can also extract data using logical conditions or pattern matching. Subscripting is also used to assign values to data object elements.
The syntax for data subscripting can take several forms depending on data structure and data object type. Examples are provided below.
Posted in R Data Objects, R Programming
Leave a comment
R Dates and Times
Preprocessing work to maintain R dates and times requires synchronize of data and formats across data sources. R dates and times justify care and attention.
Current Date/Time in R
The function date(), Sys.date() and Sys.time() all return a character string of the current system data and time:
1 2 3 4 5 6 7 8 
> date() [1] "Tue Oct 22 18:43:27 2013" > Sys.Date() [1] "20131022" > Sys.time() [1] "20131022 18:45:54 AST" 
Each of these functions returns a slightly different result, which raises the obvious question how best to manage and format dates in large data objects?
Posted in R Data Objects, R Programming
Leave a comment
Factors in R
Categorical (e.g. qualitative) data are represented as factors in R. Factors display as character strings (e.g. labels), but are stored as integers (e.g. levels).
Creating Factors in R
Factors may be created by using the factor() or as.factor() function:
1 2 3 4 5 6 7 8 9 10 
# Create data object using factor() > age < factor(c(1, 1, 2, 2, 1, 3, 1, 2), labels = c("2035yrs", "3555yrs", "55+yrs")) > age [1] 2035yrs 2035yrs 3555yrs 3555yrs 2035yrs 55+yrs 2035yrs 3555yrs # Create data object using as.factor() > age < c("2035yrs", "2035yrs", "3555yrs", "3555yrs", "2035yrs", "55+yrs", "2035yrs", "3555yrs") > age < as.factor(age) > age [1] 2035yrs 2035yrs 3555yrs 3555yrs 2035yrs 55+yrs 2035yrs 3555yrs 
Note that it is not possible to assign labels to the factor levels within the function as.factor().
Another way to create factors in R is to split a data object into category groups and then call the factor() function:
Posted in R Data Objects, R Data Syntax
Leave a comment
R Lists
R lists are a general data object made up of components, where each component is itself a data object that can be any mode or type. The length() of a list is the number of components. Lists are very flexible and a convenient structure for packaging or storing different kinds of data in one object. However, for large data, array structures are preferred based on operational runtimes.
Posted in R Data Objects
Leave a comment
Local vs Global Objects
Local vs global objects in R serve to distinguish temporary and permanent data.
Local Objects and Frames
Data objects assigned within the body of a function are temporary. That is, they are local to the function only. Local objects have no effect outside the function, and they disappear when function evaluation is complete.
Posted in R Data Objects, R Programming
Leave a comment
R Matrices
R matrices are twodimensional vectors. A matrix starts with a vector and then adds dimension or dim information (e.g. rows and columns), which is stored in a memory slot for the matrix class. The optional slot, .dimnames, holds row and column names (and their analogues for higher dimension arrays).
Initializing R Matrices
Initializing a matrix is similar to vector creation:
1 2 3 4 5 
> matrix(numeric(9), ncol=3) [,1][,2][,3] [1,] 0 0 0 [2,] 0 0 0 [3,] 0 0 0 
A unit matrix is easy to specify with the diag() function:
1 2 3 4 5 
> diag(3) [,1][,2][,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 
Posted in R Data Objects
Leave a comment
R Data Import
Introduction
Quantitative analysis depends on the ability to load and manage many different types of data and file formats. There are many R data import functions. Some functions ship with base R and others can be found in R packages.
Data Available in R
R is preinstalled with many data sets in the datasets package, which is included in the base distribution of R. R datasets are automatically loaded when the application is started. A list of all data sets in the package is obtained using the following command:
Posted in R Data Import, R Data Objects
Leave a comment
R Data Objects
Chapter Outline
Understanding R data objects is core to R programming. The following sections provide practical insight into use of R data objects by type.
Vectors: 1 dimensional row and column vectors
Matrices: 2 dimensional rectangular data
Arrays: Ndimensional data
Lists: Mixed mode data by component
Factors: Qualitative data objects
Data Frames: Mixed mode data by column
Posted in R Data Objects
Leave a comment
R Vectors
R vectors are commonly applied in mathematics, science and engineering. A vector space is a structure formed by a collection of elements. The most common interpretation of a vector is to represent location in the space of real numbers. Similarly, vectors depict physical quantities that have both magnitude and direction, such as force or wind speed.
Posted in R Data Objects
Leave a comment
Data Concatenation and Coercion in R
Data concatenation and coercion are common operations in R.
Data Concatenation
The concatenate c() function is used to combine elements into a vector.
1 2 3 4 5 
> c(T, F, T) [1] T F T > c(8.3, 9.2, 11) [1] 8.3 9.2 11.0 
When elements are combined from different classes, the c() function coerces to a common type, which is the type of the returned value:
1 2 3 4 
> x < c(100, "A", TRUE, as(1, "complex")) > x [1] "100" "A" "TRUE" "1+0i" > class(x) [1] "character" 
Posted in R Data Objects, R Data Syntax
Leave a comment
Data Formatting in R
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 startup. 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.
Posted in R Data Objects, R Data Syntax
Leave a comment