Andrie de Vries is a leading R expert and Business Services Director for Revolution Analytics. With over 20 years of experience, he provides consulting and training services in the use of R.
R is more than just a statistical programming language. It’s also a powerful tool for all kinds of data processing and manipulation, used by a community of programmers and users, academics, and practitioners.To get the most out of R, you need to know how to access the R Help files and find help from other sources.
The rbind() function in the R programming language conveniently adds the names of the vectors to the rows of the matrix. You name the values in a vector, and you can do something very similar with rows and columns in a matrix.For that, you have the functions rownames() and colnames(). Guess which one does what?
In the R programming language, a conversion from a matrix to a data frame can’t be used to construct a data frame with different types of values. If you combine both numeric and character data in a matrix, for example, everything will be converted to character.You can construct a data frame from scratch, though, using the data.
One important difference between a matrix and a data frame in R is that data frames always have named observations. Whereas the rownames() function returns NULL if you didn’t specify the row names of a matrix, it will always give a result in the case of a data frame.
Check the outcome of the following code:
> rownames(employ.
Many tests that you run in R return an htest object. That type of object is basically a list with all the information about the test that has been carried out. All these htest objects contain at least a component statistic with the value of the statistic and a component p.value with the value of the p-value.
You can see this easily if you look at the structure of the returned object.
Base R has a function, reshape(), that works fine for data reshaping. However, the original author of this function had in mind a specific use case for reshaping: so-called longitudinal data.
Longitudinal research takes repeated observations of a research subject over a period of time. For this reason, longitudinal data typically has the variables associated with time.
The first element of a ggplot2 layer is the data. There is only one rule in R for supplying data to ggplot(): Your data must be in the form of a data frame. This is different from base graphics, which allow plotting of data in vectors, matrices, and other structures.
You can use the built-in dataset quakes. This dataset is a data frame with information about earthquakes near Fiji.
When talking about reshaping data in R, it’s important to recognize data in long and wide formats. These visual metaphors describe two ways of representing the same information. It’s helpful to know these formats when using R.
You can recognize data in wide format by the fact that columns generally represent groups.
After data, mapping, and geoms, the fourth element of a ggplot2 layer in R describes how the data should be summarized. In ggplot2, you refer to this statistical summary as a stat.
One very convenient feature of ggplot2 is its range of functions to summarize your data in the plot. This means that you often don’t have to pre-summarize your data.
Many people who start with R get confused by lists in the beginning. There’s really no need for that — a list has only two important parts: the components and the names. And in the case of unnamed lists, you don’t even have to worry about the latter. But if you look at the structure of baskets.list in the following output, you can see why people often shy away from lists.