concept number in category R

This is an excerpt from Manning's book R in Action, Third Edition.
A number of R functions let you manipulate packages. To install a package for the first time, use the install.packages() command. For example, the gclus package contains functions for creating enhanced scatter plots. You can download and install the package with the command install.packages("gclus").
First, the mean illiteracy rate is calculated for each region #1. Next, the means are plotted in sorted in ascending order as bars #2. Normally, the geom_bar() function calculates and plots cell counts but adding the stat="identity" option forces the function to plot the numbers provided (means in this case). The reorder() function is used to order the bars by increasing mean illiteracy.
Figure 6.13. Tree map displaying the distribution of car manufacturers in the mpg data set. Rectangle size is proportional to the number of cars from each manufacturer.
![]()
The fill option fills the box plots with a red color. In a standard box plot, the box width has no meaning. Adding varwidth=TRUE, draws box widths proportional to the square roots of the number of observations in each group.
You can see in figure 6.21 that the median car mileage for four-, six-, and eight-cylinder cars differs. Mileage clearly decreases with number of cylinders. Additionally, there are fewer 8-cylinder cars, than 4- or 6-cylinder cars (although the difference is subtle).
Figure 6.21 Notched box plots for car mileage vs. number of cylinders
![]()
Finally, you can produce box plots for more than one grouping factor. The following code provides box plots for city miles per gallon versus the number of cylinders by year (see figure 6.21). The scale_fill_manual() function has been added in order to customize the fill colors.
ggplot(cars, aes(x=Cylinders, y=cty, fill=Year)) + geom_boxplot() + labs(x="Number of Cylinders", y="Miles Per Gallon", title="City Mileage by # Cylinders and Year") + scale_fill_manual(values=c("gold", "green"))From figure 6.22, it’s again clear that median mileage decreases with number of cylinders. Additionally, for each group, mileage has increased between 1999 and 2008.
Figure 6.22 Box plots for car mileage vs. year and number of cylinders
![]()

This is an excerpt from Manning's book Exploring Data with R.
R is currently developed by the R Development Core Team and, at the time of this writing, the latest R release is 3.6. R has a package system that allows users to write and distribute their own packages of functions. The number of user-created packages at this point is well over 10,000 and that figure is part of the reason why R has so much functionality. A more detailed look at the R landscape is available in the Appendix (Section A1.1).
Want to give that unnamed list some names? We can use the names() function to do that. Just ensure that the number of names provided is the same length as the list (check that with the length() function).
Figure 9.5. A bar plot that shows the number of cities in the dataset that are part of each state. We need to order the bars, however, to make this a more effective visualization (it’s hard to parse at present).
![]()
Listing 11.19. A violin plot can be more interpretable than overlaid points on a box plot if the number of data points is overwhelming
```{r pitchfork_violin_plot} pitchfork %>% mutate(year = factor(year)) %>% ggplot() + geom_violin( aes(x = year, y = score, fill = year), draw_quantiles = c(0.25, 0.50, 0.75), show.legend = FALSE ) + scale_fill_viridis_d(alpha = 0.5, option = "E") ```

This is an excerpt from Manning's book Machine Learning with R, the tidyverse, and mlr.
A vector is a set of numbers that encodes both magnitude and direction. Imagine a coordinate system with an x-axis and a y-axis, as shown in figure A.2. If we pick a point on the coordinate system, that point will have a value for each axis: let’s say x = 3 and y = 5. We can represent this point as the vector (3,5). The vector encodes magnitude, because we can calculate the distance between the point defined by this vector and the origin of the coordinate system (0,0). The vector also encodes direction, because if we draw a line connecting the origin (0,0) to this point (3,5), we can calculate the angle between this line and the axes of the coordinate space. Figure A.2 is an example of a two-dimensional vector, but vectors can have as many dimensions as we like.
Listing 9.9. Tuning the number of predictors to retain
lmParamSpace <- makeParamSet( makeIntegerParam("fw.abs", lower = 1, upper = 12) ) gridSearch <- makeTuneControlGrid() kFold <- makeResampleDesc("CV", iters = 10) tunedFeats <- tuneParams(filterWrapper, task = ozoneTask, resampling = kFold, par.set = lmParamSpace, control = gridSearch) tunedFeats Tune result: Op. pars: fw.abs=10 mse.test.mean=20.8834
The intuition here is that if we maintain the same diameter of our clusters but move the closest pair apart, the Dunn index will get larger. Conversely, if we maintain the same distance between cluster centroids but shrink the diameter of the clusters (by making the clusters denser), the Dunn index will also increase. As such, the number of clusters resulting in the largest Dunn index is the one that results in the largest minimum distance between clusters and the smallest maximum distance between cases within a cluster.
The pseudo F statistic is a ratio of the between-cluster sum of squares to the within-cluster sum of squares (see figure 16.6). The between-cluster sum of squares is the squared difference between each cluster centroid and the grand centroid (the centroid of the data as if it was all in one big cluster), weighted by the number of cases in that cluster, added up across each cluster. This is another way of measuring how separated the clusters are from each other (the farther the cluster centroids are from each other, the smaller the between sum of squares will be). The within-cluster sum of squares is the squared difference between each case and its cluster’s centroid, added up across each cluster. This is another way of measuring the variance or dispersion within each cluster (the denser each cluster is, the smaller the within-cluster sum of squares will be).
Figure 16.6. The pseudo F statistic is the ratio of the between-cluster sum of squares (right-side plot) to the within-cluster sum of squares (left-side plot). The grand centroid is shown as a square in the right-side plot.
![]()
Because the pseudo F statistic is also a ratio, if we maintain the same cluster variance but move the clusters farther apart, the pseudo F statistic will increase. Conversely, if we maintain the same separation between the cluster centroids but make the clusters more spread out, the pseudo F statistic will decrease. As such, the number of clusters that results in the largest pseudo F statistic is, in theory, the one that maximizes the separation of the clusters.

This is an excerpt from Manning's book Practical Data Science with R, Second Edition.
Data science is not performed in a vacuum. It’s a collaborative effort that draws on a number of roles, skills, and tools. Before we talk about the process itself, let’s look at the roles that must be filled in a successful project. Project management has been a central concern of software engineering for a long time, so we can look there for guidance. In defining the roles here, we’ve borrowed some ideas from Fredrick Brooks’ “surgical team” perspective on software development, as described in The Mythical Man-Month: Essays on Software Engineering (Addison-Wesley, 1995). We also borrowed ideas from the agile software development paradigm.
Manhattan distance measures distance in the number of horizontal and vertical units it takes to get from one (real-valued) point to the other (no diagonal moves). This is also known as L1 distance (and squared Euclidean distance is L2 distance ).
An important question when evaluating clusters is whether a given cluster is “real”—does the cluster represent actual structure in the data, or is it an artifact of the clustering algorithm? As you’ll see, this is especially important with clustering algorithms like k-means, where the user has to specify the number of clusters beforehand. It’s been our experience that clustering algorithms will often produce several clusters that represent actual structure or relationships in the data, and then one or two clusters that are buckets that represent “other” or “miscellaneous.” Clusters of "other" tend to be made up of data points that have no real relationship to each other; they just don’t fit anywhere else.
One way to assess whether a cluster represents true structure is to see if the cluster holds up under plausible variations in the dataset. The fpc package has a function called clusterboot() that uses bootstrap resampling to evaluate how stable a given cluster is.[1] clusterboot() is an integrated function that both performs the clustering and evaluates the final produced clusters. It has interfaces to a number of R clustering algorithms, including both hclust and kmeans.
Listing B.8. Working with the theoretical binomial distribution
p = 0.5 # the percentage of females in this student population class_size <- 20 # size of a classroom numclasses <- 100 # how many classrooms we observe # what might a typical outcome look like? numFemales <- rbinom(numclasses, class_size, p) #1 # the theoretical counts (not necessarily integral) probs <- dbinom(0:class_size, class_size, p) tcount <- numclasses*probs # the obvious way to plot this is with histogram or geom_bar # but this might just look better zero <- function(x) {0} # a dummy function that returns only 0 ggplot(data.frame(number_of_girls = numFemales, dummy = 1), aes(x = number_of_girls, y = dummy)) + # count the number of times you see x heads stat_summary(fun.y = "sum", geom = "point", size=2) + #2 stat_summary(fun.ymax = "sum", fun.ymin = "zero", geom = "linerange") + # superimpose the theoretical number of times you see x heads geom_line(data = data.frame(x = 0:class_size, y = tcount), aes(x = x, y = y), linetype = 2) + scale_x_continuous(breaks = 0:class_size, labels = 0:class_size) + scale_y_continuous("number of classrooms")

This is an excerpt from Manning's book Beyond Spreadsheets with R.
There are other ways to input numbers. If you need to input very large numbers such as millions, you can use something similar to scientific notation, where you specify an exponent in the power of tens with an
eorE. The number 1,000,000 has six zeroes, so 5 million can be entered like this:5e6 #1 #> [1] 5e+06 #1 Or 5E6The part of the number in front of
eorEcan have decimal places if needed, so 1,200,000 (“one point two million”) can be entered as follows:
Figure 2.10 Various ways to format numbers in a spreadsheet program
![]()

This is an excerpt from Manning's book R in Action, Second Edition: Data analysis and graphics with R.
A number of R functions let you manipulate packages. To install a package for the first time, use the install.packages() command. For example, install.packages() without options brings up a list of CRAN mirror sites. Once you select a site, you’re presented with a list of all available packages. Selecting one downloads and installs it. If you know what package you want to install, you can do so directly by providing it as an argument to the function. For example, the gclus package contains functions for creating enhanced scatter plots. You can download and install the package with the command install.packages("gclus").
Listing 6.9. Box plots for two crossed factors
mtcars$cyl.f <- factor(mtcars$cyl, #1 levels=c(4,6,8), #1 labels=c("4","6","8")) #1 mtcars$am.f <- factor(mtcars$am, #2 levels=c(0,1), #2 labels=c("auto", "standard")) #2 boxplot(mpg ~ am.f *cyl.f, #3 data=mtcars, #3 varwidth=TRUE, #3 col=c("gold","darkgreen"), #3 main="MPG Distribution by Auto Type", #3 xlab="Auto Type", ylab="Miles Per Gallon") #3 #1: Creates a factor for the number of cylinders #2: Creates a factor for transmission type #3: Generates the box plot
The methods described in this chapter require large samples to derive stable solutions. What constitutes an adequate sample size is somewhat complicated. Until recently, analysts used rules of thumb like “factor analysis requires 5–10 times as many subjects as variables.” Recent studies suggest that the required sample size depends on the number of factors, the number of variables associated with each factor, and how well the set of factors explains the variance in the variables (Bandalos and Boehm-Kaufman, 2009). I’ll go out on a limb and say that if you have several hundred observations, you’re probably safe. In this chapter, we’ll look at artificially small problems in order to keep the output (and page count) manageable.
Basing the number of components on prior experience and theory Selecting the number of components needed to account for some threshold cumulative amount of variance in the variables (for example, 80%) Selecting the number of components to retain by examining the eigenvalues of the k × k correlation matrix among the variables