R Tutorial: Reading multivariate data

Surajit Ray shows how to read multivariate datasets into R using read.table and read.csv. He walks through importing the IRIS dataset from a URL and a local birth-weight CSV, then inspecting the data with head, names, and str functions.
Chapters
Hi, I'm Surajit Ray and I teach at the University of Glasgow in the uk. I will be your instructor for this course on multivariate probability distributions in R. Multivariate distributions are designed to describe the probability distributions of more than one random variable at the same time. Since the variables are often correlated, exploring them individually would only provide limited insights. In this course you will learn how to read and analyze multivariate data. You will explore several plotting techniques and learn how to use common statistical distributions including the Gaussian distribution and T distribution. Lastly, you will learn about techniques for dealing with high dimensional data such as principal component analysis. Multivariate data is mostly rectangular in shape, meaning it is organized by rows and columns, where the rows represent the individual observations and the columns represent the individual variables. Data sets may or may not include row names or numbers or column headers. We should also be aware that some data sets might come with missing entries. First, let us look at the IRIS dataset from the Cambridge University website. The iris dataset contains three iris species with 50 samples from each species. The first four columns list the length and width of the sepals and petals, and the last column contains the species name. This data set does not include the column names and the separated between columns is a white space. In the second data set, the birth weight is stored locally. The first row of this data set contains the column names and the first column contains the row numbers. The entries are separated by commas. We will learn how to read in these two data sets in the next slides. First, assign the URL to an object irisurl. Then use readdoctable with irisurl as the first argument, specify that the separator is a white space and set header equals to false. Since the data set does not include column names, the data set is called irisraw. If the data set is stored locally, replace the URL name with a filename using head with n equals 4 as the argument. To view the first four rows of the data set, we see that R has provided generic column names V1 through V5 and Rownames V1 through 4. We can assign column names to each of the variables using the colnems function. Now the head function displays the new column names. The names function can be used to check the current names of the columns. Specific columns can be accessed by their column number or by the column names. The last column species represents the three different Setosa, Virginica, and Versicolor. However, the different species are currently coded as numeric variables with values from 1 through 3. We modify the last column to be a categorical variable, which R calls a factor using as factor function. The str function now shows that species is a factor with three levels. Although the variable was changed to a factor, the different species are still coded as integers. The recode function from the char library allows us to rename the integers 1, 2, and 3 to the actual species names. In contrast to the IRIS dataset, the birth weight dataset has clearly defined names for the columns and rows, and the entries are separated by commas. We use read CSV to read the data and specify that the first column contains the row numbers using the argument row names one. Now let's read a dataset from an external.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







