Factors form the basis for many of R’s powerful operations, including many of those performed on tabular data. The motivation for factors comes from the notion of nominal, or categorical, variables in statistics. These values are nonnumerical in nature, corresponding to categories such as Democrat, Republican, and Unaffiliated, although they may be coded using numbers.
Factors and Levels An R factor might be viewed simply as a vector with a bit more information added (though, as seen below, it’s different from this internally). That extra information consists of a record of the distinct values in that vector, called levels. Here’s an example:
#factor() - are useful when you want to display character vectors in a non-alphabetical order.
x <- c(5,12,13,12)
xf <- factor(x)
xf
## [1] 5 12 13 12
## Levels: 5 12 13
The distinct values in xf—5, 12, and 13—are the levels here. Let’s take a look inside:
#Will give you all the information that is in the (xf) factor
str(xf)
## Factor w/ 3 levels "5","12","13": 1 2 3 2
#unclass() - returns (a copy of) its argument with its class information removed
unclass(xf)
## [1] 1 2 3 2
## attr(,"levels")
## [1] "5" "12" "13"
#Length() - tells you the length of a vector/ variable being inputed
length(xf)
## [1] 4
We can anticipate future new levels, as seen here:
#levels() - are a predefined set of values, known as levels. By default, R always sorts levels in alphabetical order.
#so you are making the variables be listed in th order that the levels are stated.
x <- c(5,12,13,12)
xff <- factor(x,levels=c(5,12,13,88))
xff
## [1] 5 12 13 12
## Levels: 5 12 13 88
#88 was assigned to be the 2 value in the xff factor. When xff was printed again, it was printed the levels order that it was assigned before.
xff[2] <- 88
x
## [1] 5 12 13 12
xff
## [1] 5 88 13 12
## Levels: 5 12 13 88
Originally, xff did not contain the value 88, but in defining it, we allowed for that future possibility. Later, we did indeed add the value.
Common Functions Used with Factors
With factors, we have yet another member of the family of apply functions, tapply. We’ll look at that function, as well as two other functions commonly used with factors: split() and by().
The operation performed by tapply() is to (temporarily) split x into groups, each group corresponding to a level of the factor (or a combination of levels of the factors in the case of multiple factors), and then apply g() to the resulting subvectors of x. Here’s a little example:
#The arrays ages, affils are combined together and then applied a mean function to get the result.
#R- has 2 values to combine the means, D has 3 values to combine the mean, and U has 1 value in which not mean is calculated.
ages <- c(25,26,55,37,21,42)
affils <- c("R","D","D","R","U","D")
tapply(ages,affils,mean)
## D R U
## 41 31 21
getwd()
## [1] "/cloud/project"
Let’s look at what happened. The function tapply() treated the vector (“R”,“D”,“D”,“R”,“U”,“D”) as a factor with levels “D”, “R”, and “U”. It noted that “D” occurred in indices 2, 3 and 6; “R” occurred in indices 1 and 4; and “U” occurred in index 5. For convenience, let’s refer to the three index vectors (2,3,6), (1,4), and (5) as x, y, and z, respectively. Then tapply() computed mean(u[x]), mean(u[y]), and mean(u[z]) and returned those means in a three-element vector. And that vector’s element names are “D”, “R”, and “U”, reflecting the factor levels that were used by tapply().
What if we have two or more factors? Then each factor yields a set of groups, as in the preceding example, and the groups are ANDed together. As an example, suppose that we have an economic data set that includes variables for gender, age, and income. Here, the call tapply(x,f,g) might have x as income and f as a pair of factors: one for gender and the other coding whether the person is older or younger than 25. We may be interested in finding mean income, broken down by gender and age. If we set g() to be mean(), tapply() will return the mean incomes in each of four subgroups:
• Male and under 25 years old • Female and under 25 years old • Male and over 25 years old • Female and over 25 years old
#D data frame was created to have 3 columns and 6 rows.
d <- data.frame(list(gender=c("M","M","F","M","F","F"),age=c(47,59,21,32,33,24),income=c(55000,88000,32450,76500,123000,45650)))
#Display the D data frame,
d
#'$' adds a new column to the data frame called 'over25'
#ifelse() - is the alternative and shorthand form of the R if-else statement. Also, it uses the 'vectorized' technique, which makes the operation faster
# It states that if the age is over 25 then place '1', if not place '0'.
d$over25 <- ifelse(d$age > 25,1,0)
d
# There are 2 instances where F,0 exits, 1 instance for F,1; NO instance for M,0, and 3 sintances for M,1
# for those instances get the mean
tapply(d$income,list(d$gender,d$over25),mean)
## 0 1
## F 39050 123000.00
## M NA 73166.67
We specified two factors, gender and indicator variable for age over or under 25. Since each of these factors has two levels, tapply() partitioned the income data into four groups, one for each combination of gender and age, and then applied to mean() function to each group.
The split() Function
In contrast to tapply(), which splits a vector into groups and then applies a specified function on each group, split() stops at that first stage, just forming the groups. The basic form, without bells and whistles, is split(x,f), with x and f playing roles similar to those in the call tapply(x,f,g); that is, x being a vector or data frame and f being a factor or a list of factors. The action is to split x into groups, which are returned in a list. (Note that x is allowed to be a data frame with split() but not with tapply().
Let’s try it out with our earlier example.
# split() - divides a vector or data frame into groups according to the function's parameters.
#shows you how the columns were split above in order to then get a calculation.
split(d$income,list(d$gender,d$over25))
## $F.0
## [1] 32450 45650
##
## $M.0
## numeric(0)
##
## $F.1
## [1] 123000
##
## $M.1
## [1] 55000 88000 76500
The output of split() is a list, and recall that list components are denoted by dollar signs. So the last vector, for example, was named “M.1” to indicate that it was the result of combining “M” in the first factor and 1 in the second.
As another illustration, consider our abalone example from previous in class activitities (2.9.2). We wanted to determine the indices of the vector elements corresponding to male, female, and infant. The data in that little example consisted of the seven-observation vector (“M”,“F”,“F”,“I”,“M”,“M”,“F”), assigned to g. We can do this in a flash with split().
#split (x,f)
#X: Name of the data frame or vector to be divided into groups
#F: A criterion used to classify people into groups.
#shows you in what position each variable in the g array is located.
#M-
g <- c("M","F","F","I","M","M","F")
split(1:7,g)
## $F
## [1] 2 3 7
##
## $I
## [1] 4
##
## $M
## [1] 1 5 6
Let’s dissect this step-by-step. The vector g, taken as a factor, has three levels: “M”, “F”, and “I”. The indices corresponding to the first level are 1, 5, and 6, which means that g[1], g[5], and g[6] all have the value “M”. So, R sets the M component of the output to elements 1, 5, and 6 of 1:7, which is the vector (1,5,6).
Working with Tables
To begin exploring R tables, consider this example:
#U- purpose is soley to be able to use the tapply function; tapply needs two variables in order to calculate(aggregate) the data. so it takes U and fl- then only calculates fl because its the primary dataset for the function(one with characters) to calculate the formula/ function for.
u <- c(22,8,33,6,8,29,-2)
fl <- list(c(5,12,13,12,13,5,13),c("a","bc","a","a","bc","a","a"))
tapply(u,fl,length)
## a bc
## 5 2 NA
## 12 1 1
## 13 2 1
Here, tapply() again
temporarily breaks u into subvectors, as you saw earlier,and then
applies the length() function to each subvector. (Note that this is
independent of what’s in u. Our focus now is purely on the factors.)
Those subvector lengths are the counts of the occurrences of each of the
3 × 2 = 6 combinations of the two factors. For instance, 5 occurred
twice with “a” and not at all with “bc”; hence the entries 2 and NA in
the first row of the output.In statistics, this is called a contingency
table.
There is one problem in this example: the NA value. It really should be 0, meaning that in no cases did the first factor have level 5 and the second have level “bc”. The table() function creates contingency tables correctly.
#creates a Table correctly,
#table() - performs a tabulation of categorical variable and gives its frequency as output.
table(fl)
## fl.2
## fl.1 a bc
## 5 2 0
## 12 1 1
## 13 2 1
The first argument in a call to table() is either a factor or a list of factors. The two factors here were (5,12,13,12,13,5,13) and (“a”,“bc”,“a”,“a”,“bc”,“a”,“a”). In this case, an object that is interpretable as a factor is counted as one.
#similar to the matrix created above, the difference is that the rows as labeled as the fl.1 values and the columns are labeled as the fl.2 values.