March 3, 2015 Leave a comment
This post is a sequel to my earlier post dated December 9th 2014
General background in R
I. R datatypes are;
a. numeric Numeric data (approximations of the real numbers, R)
b. integer Integer data (whole numbers, Z)
c. factor Categorical data (simple classifications, like gender)
d. ordered Ordinal data (ordered classifications, like educational level)
e. character Character data (strings)
f. raw Binary data
Variable types and indexing techniques
The most basic variable in R is a vector. An R vector is a sequence of values of the same type.
Elements of a vector can be selected or replaced using the square bracket operator [ ] . The square brackets accept either a vector of names, index numbers, or a logical.
A list is a generalization of a vector in that it can contain objects of different types, including other lists. There are two ways to index a list. The single bracket operator always returns a sub-list of the indexed list. That is, the resulting type is again a list. The double bracket operator ([[ ]] ) may only result in a single item, and it returns the object in the list itself. Besides indexing, the dollar operator $ can be used to retrieve a single element. To understand the above, check the results of the following statements.
AL <- list(x = c(1:5), y = c(“a”, “b”, “c”), z = capColor)
I.C. Data Frame
A data.frame is not much more than a list of vectors, possibly of different types, but with every vector (now columns) of the same length. Since data.frames are a type of list, indexing them with a single index returns a sub-data.frame; that is, a data.frame with less columns.
Likewise, the dollar operator returns a vector, not a sub-data.frame. Rows can be indexed using two indices in the bracket operator, separated by a comma. The first index indicates rows,
the second indicates columns. If one of the indices is left out, no selection is made (so everything is returned). It is important to realize that the result of a two-index selection is
simplified by R as much as possible. Hence, selecting a single column using a two-index results in a vector. This behaviour may be switched off using drop=FALSE as an extra parameter. Here
are some short examples demonstrating the above.
# the following command creates 10 obs in 3 variables
>d <- data.frame(x = 1:10, y = letters[1:10], z = LETTERS[1:10])
> d[, 1]
## 1 2 3 4 5 6 7 8 9 10
> d[, “x”, drop = FALSE]
## x z
1 1 A
2 2 B
3 3 C
4 4 D
5 5 E
6 6 F
7 7 G
8 8 H
9 9 I
10 10 J
>d[d$x > 3, “y”, drop = FALSE]
Error: unexpected ‘>’ in “>”
I.D. Special values
R has a number of Special values that are exceptions to the
normal values of a type. These are NA, NULL, ±Inf and NaN.
i. NA Stands for not available. NA is a placeholder for a missing value.
The function is.na can be used to detect NA’s.
> sum(c(NA, 1, 2))
ii. NULL You may think of NULL as the empty set from mathematics. NULL is special since it has no class (its class is NULL) and has length 0 so it does not take up any space in a vector. The function is.null can be used to detect NULL variables.
iii. Inf Stands for infinity and only applies to vectors of class numeric. A vector of class integer can never be Inf.
iv. NaN Stands for not a number. This is generally the result of a calculation of which the result is unknown, but it is surely not a number. In particular operations like 0/0, Inf-Inf and Inf/Inf result in NaN. Technically, NaN is of class numeric, which may seem odd since it is used to indicate that something is not numeric. Computations involving numbers and NaN always result in NaN
The function is.nan can be used to detect NaN’s.
2. What is technically correct data in R?
In the case of R, we define technically correct data as a data set that
– is stored in a data.frame with suitable columns names, and
– each column of the data.frame is of the R type that adequately represents the value domain of the variable in the column.
Numeric data should be stored as numeric or integer, textual
data should be stored as character and categorical data should be stored as a factor or ordered vector, with the appropriate levels
Best practice. A freshly read data.frame should always be inspected with functions like head, str, and summary.
2.1 Data preprocessing with R
A: Consider the following example. Create a .csv file with the following data in it. Save it as datafile.csv
Now when you read the file using >read.csv command like
person <- read.csv(file = “/datafile.csv”, header = FALSE
, col.names = c(“age”,”height”) )
The command when executed creates a data.frame but the problem comes because of a malformed numerical value in the data like 2.3*. , causing R to interpret the whole column as a
text variable. Moreover, by default text variables are converted to factor, so we are now stuck with a height variable expressed as levels in a categorical variable:
## ‘data.frame': 4 obs. of 2 variables:
## $ age : int 21 42 18 21
## $ height: Factor w/ 3 levels “5.7*”,”5.9″,”6.0″: 3 2 1 NA
Using colClasses, we can force R to either interpret the columns in the way we want or throw an error when this is not possible.
read.csv(“files/datafile.csv”, header=FALSE, colClasses=c (‘numeric’,’numeric’))
## Error: scan() expected ‘a real’, got ‘2.3*’
As an alternative, columns can be read in as character by setting stringsAsFactors=FALSE.
dat <- read.csv(file = “datafile.csv”, header = FALSE, col.names = c(“age”,”height”), stringsAsFactors=FALSE)
dat$height <- as.numeric(dat$height)
## Warning: NAs introduced by coercion
B. Cleaning a text file
Assume you have a text file with the following contents;
%% Data on the Amigo Brothers
% Names, birth and death dates
To remove %% sign use the grepl command as
> A<- grepl(“^%”, txt)
C. Factors in R
In R, the value of categorical variables is stored in factor variables. A factor is an integer vector endowed with a table specifying what integer value corresponds to what level. The values in this translation table can be requested with the levels function.
> f <- factor(c(“a”, “b”, “a”, “a”, “c”))
##  “a” “b” “c”
For example, suppose we read in a vector where 1 stands for male, 2 stands for female and 0 stands for
unknown. Conversion to a factor variable can be done as in the example below
##  “a” “b” “c”
>gender <- c(2, 1, 1, 2, 0, 1, 1)
# recoding table, stored in a simple vector
>recode <- c(male = 1, female = 2)
(gender <- factor(gender, levels = recode, labels = names(recode)))
## female male male female <NA> male male
##Levels: male female
Note that we do not explicitly need to set NA as a label. Every integer value that is encountered in the first argument, but not in the levels argument will be regarded missing. Levels in a factor variable have no natural ordering.
de Jonge, E., & van der Loo, M. (2013). An introduction to data cleaning with R: Technical Report 201313, Statistics Netherlands, 2013. URL http://www. cbs. nl/nl-NL/menu/methoden/onderzoek-methoden/discussionpapers/archief/2013/default. htm.