### Login

### Today in Energy

### Tags

Animation (3) Data (12) Data Science (12) Distributions (1) Economics (5) Engineering (1) Faster R (2) GDAL (5) ggplot2 (19) Git/GitHub (3) LaTeX (12) Linux (9) Misc Tricks (3) Modeling (6) Projects (1) R Basics (17) R Colors (9) R Data Import (6) R Data Objects (18) R Data Syntax (11) R Graphics (20) R Packages (3) R Programming (26) Scientific Computing (4) Spatial Analysis (6) Ubuntu (1) Web Scrapping (2) Website (5)### Archives

# Category Archives: R Data Objects

## Data Frames in R

Arrays generalize the dimensional aspect of a matrix and assume only one data mode. Data frames in R generalize the mode of a matrix and allow mode mixing. Data frames with mode mixing are are the most widely used data objects in R.

**Creating Data Frames in R**

**Creating Data Frames in R**

You can create data frames in R several ways:

- importData() and read.table() both read data from an external file as a data.frame

Read more...

*(1034 words, estimated 4:08 mins reading time)*
Posted in R Data Objects
Comments Off on Data Frames in R

## Geospatial Data and Mapping in R

I share slides presented at a recent meeting of Doha R users on geospatial data and mapping in R .

Geospatial Data and Mapping in R (225 downloads)

Permanent link to this post (30 words, 1 image, estimated 7 secs reading time)

Posted in Data Science, R Data Objects, R Data Syntax, R Programming, Spatial Analysis
Comments Off on Geospatial Data and Mapping in R

## 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*

*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:

Read more...

*(115 words, estimated 28 secs reading time)*
Posted in R Basics, R Data Objects
Comments Off on Data Modes and Classes in R

## Data Object Management

*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. |

Read more...

*(278 words, estimated 1:07 mins reading time)*
Posted in R Basics, R Data Objects
Comments Off on Data Object Management

## 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*

*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`:`

Read more...

*(172 words, estimated 41 secs reading time)*
Posted in R Basics, R Data Objects
Comments Off on Data Sequences and Repetition in R

## 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*

*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:

Read more...

*(414 words, estimated 1:39 mins reading time)*
Posted in R Data Objects, R Programming
Comments Off on Data Sorting in R

## R Data Subscripting

**Intro to R Data Subscripting**

**Intro to R Data Subscripting**

Data subscripting in R is a key “motor skill” to extract data by row, column or element. Subscripting is achieved using numeric, character, 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.

Read more...

*(337 words, estimated 1:21 mins reading time)*
Posted in R Basics, R Data Objects
Comments Off on R Data Subscripting

## 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*

*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] "2013-10-22" > Sys.time() [1] "2013-10-22 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?

Read more...

*(943 words, estimated 3:46 mins reading time)*
Posted in R Basics, R Data Objects
Comments Off on R Dates and Times

## 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*

*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("20-35yrs", "35-55yrs", "55+yrs")) > age [1] 20-35yrs 20-35yrs 35-55yrs 35-55yrs 20-35yrs 55+yrs 20-35yrs 35-55yrs # Create data object using as.factor() > age <- c("20-35yrs", "20-35yrs", "35-55yrs", "35-55yrs", "20-35yrs", "55+yrs", "20-35yrs", "35-55yrs") > age <- as.factor(age) > age [1] 20-35yrs 20-35yrs 35-55yrs 35-55yrs 20-35yrs 55+yrs 20-35yrs 35-55yrs |

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:

Read more...

*(368 words, estimated 1:28 mins reading time)*
Posted in R Data Objects, R Data Syntax
Comments Off on Factors in R

## 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 run-times.

Read more...

*(720 words, estimated 2:53 mins reading time)*
Posted in R Data Objects
Comments Off on R Lists

## Local vs Global Objects

Local vs global objects in R serve to distinguish temporary and permanent data.

*Local Objects and Frames*

*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.

Read more...

*(757 words, 1 image, estimated 3:02 mins reading time)*
Posted in R Data Objects, R Programming
Comments Off on Local vs Global Objects

## R Matrices

R matrices are two-dimensional 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 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 |

Read more...

*(738 words, estimated 2:57 mins reading time)*
Posted in R Data Objects
Comments Off on R Matrices

## R Data Import

*Introduction*

*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*

*Data Available in R*

R is pre-installed 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:

Read more...

*(1751 words, 1 image, estimated 7:00 mins reading time)*
Posted in R Basics, R Data Import, R Data Objects
Comments Off on R Data Import

## 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: N-dimensional data

Lists: Mixed mode data by component

Factors: Qualitative data objects

Data Frames: Mixed mode data by column

Read more...

*(89 words, estimated 21 secs reading time)*
Posted in R Data Objects
Comments Off on R Data Objects

## 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.

Read more...

*(732 words, estimated 2:56 mins reading time)*
Posted in R Data Objects
Comments Off on R Vectors