Here we will discuss how to read data from the R library. Many R libraries contain datasets. For example, the
car package contains a
Duncan dataset that can be used for learning and implementing different R functions. To use
Duncan data, first, you have to load the
car package. Note that the
car package must be installed to make use of the
Duncan dataset. Let us make use of the
library(car) data(Duncan) attach(Duncan)
car package is not installed on your system, you can install using the following command. Note your system should be connected to the internet.
attach( ) function makes accessible each variable without writing the variable name with the respective dataset name. After attaching the Duncan dataset one can access the variable say
education instead of writing
Duncan$education. Let us make some functions on this dataset.
head( ) function will display the top six observations with their variable names in table type format. It will help to understand the structure of the dataset.
For quantitative variables, the
summary( ) function will provide five-number summary statistics with the mean value. For qualitative variables, the
summary( ) function will provide the frequency of each group.
To plot a scatter plot one can use the plot function. For example,
The scatter plot shows the strength and direction of the relationship between “Percentage of occupational incumbents in 1950 who were high school graduates’ and ‘Percentage of occupational incumbents in the 1950 US Census who earned $3,500’.
To check how many observations and columns are in a dataset, one can make use of
nrow( ) and
ncol( ) function. For example,
To get the definition of a dataset and its variable, one can read the dataset documentation:
To see the list of pre-loaded data, type the function
It is best practice to attach data only one at a time when reading data from the R library or importing from the data file. To remove a data frame from the search path, use
Try the following dataset and make use of all functions discussed in this lecture.
mtcars iris TootGrowth PlantGrowth USAarrests