Reading Data from R Library

Here we will discuss how to read data from the R library. Many R libraries contain datasets, may be called data libraries. 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 reading data from R library and make use of the Duncan dataset.

library(car)
data(Duncan)
attach(Duncan)

If the car the package is not installed on your system, you can install using the following command. Note your system should be connected to the internet.

install.packages("car")

Reading Data from R Library

The attach( ) function makes each variable accessible 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(Duncan)

The 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.

summary(Duncan)

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,

plot(education, income)
Scatter plot of Education and Income

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’.

Getting Basic Data Information

To check how many observations and columns are in a dataset, one can make use of nrow( ) and ncol( ) function. For example,

nrow(Duncan)
ncol(Duncan)

To get the definition of a dataset and its variable, one can read the dataset documentation:

?Duncan

To see the list of pre-loaded data, type the function data( ):

Reading Data from R Library
data( )

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 detach()function.

Exercise

Try the following dataset and make use of all the functions discussed in this lecture.

mtcars
iris
TootGrowth
PlantGrowth
USAarrests

SPSS Data Analysis

MCQs General Knowledge

Leave a Reply