A.2 Loading and Initial Inspection of the Database
When working on projects in R, it is very common to import databases containing information relevant to a particular study. R can import databases in several formats, including xlsx, csv, txt, STATA, and others. Once the database has been read in the relevant format, it is advisable to transform it into the native R format, that is, .RDS. This is a more efficient format specific to R.
Once the database has been loaded, R functions are used to obtain results from aggregate processing and graphs of interest. To illustrate the use of functions that produce aggregate results, we will use the BigCity database from the TeachingSampling package. This database contains a set of socioeconomic variables for 150266 people in a particular year.
The bigcity_data object contains a copy of BigCity. The head() function makes it possible to view the first rows and obtain a quick impression of the database structure.
## HHID PersonID Stratum PSU Zone Sex Age MaritalST Income
## 1 idHH00001 idPer01 idStrt001 PSU0001 Rural Male 38 Married 555
## 2 idHH00001 idPer02 idStrt001 PSU0001 Rural Female 40 Married 555
## 3 idHH00001 idPer03 idStrt001 PSU0001 Rural Female 20 Single 555
## 4 idHH00001 idPer04 idStrt001 PSU0001 Rural Male 19 Single 555
## 5 idHH00001 idPer05 idStrt001 PSU0001 Rural Male 18 Single 555
## 6 idHH00002 idPer01 idStrt001 PSU0001 Rural Male 35 Married 298
## Expenditure Employment Poverty
## 1 488 Employed NotPoor
## 2 488 Employed NotPoor
## 3 488 Inactive NotPoor
## 4 488 Employed NotPoor
## 5 488 Inactive NotPoor
## 6 217 Employed Relative
Once the database has been loaded into R, processing can begin according to the needs of each researcher. In this sense, one of the first checks performed when loading a database is to verify its dimensions; that is, to identify the number of rows and columns it contains. This can be done with the nrow function, which identifies the number of records in the database, and with the ncol function, which shows the number of variables. The computational code is as follows:
## [1] 150266
## [1] 12
A summarized way to check the number of rows and columns in a database is through the dim function, which returns a vector whose first component corresponds to the number of rows and whose second component indicates the number of columns.
## [1] 150266 12
In some cases, databases may be large, either because they contain a considerable number of observed variables or because the number of records is large. For this reason, to view them properly, it may be convenient to open them in a separate window and explore their content in tabular form. This is done using the View function, as shown below.
Another important check when loading a database into R is to identify the variables it contains. For this purpose, the names function can be used; it shows the names of the variables included in the database.
## [1] "HHID" "PersonID" "Stratum" "PSU" "Zone"
## [6] "Sex" "Age" "MaritalST" "Income" "Expenditure"
## [11] "Employment" "Poverty"
The previous output shows the names of the variables contained in the database. In this case, the database has 12 variables. The first variables correspond to identifiers and sampling design elements, such as HHID, PersonID, Stratum, PSU, and Zone. Then come sociodemographic variables, such as Sex, Age, and MaritalST. Finally, economic variables and variables related to labor or social status are included, such as Income, Expenditure, Employment, and Poverty.
To examine in greater detail the type of information stored by each variable, the str function can be used. This function compactly displays the structure of an object and its components. For our database, this function would be used as follows:
## 'data.frame': 150266 obs. of 12 variables:
## $ HHID : chr "idHH00001" "idHH00001" "idHH00001" "idHH00001" ...
## $ PersonID : chr "idPer01" "idPer02" "idPer03" "idPer04" ...
## $ Stratum : chr "idStrt001" "idStrt001" "idStrt001" "idStrt001" ...
## $ PSU : chr "PSU0001" "PSU0001" "PSU0001" "PSU0001" ...
## $ Zone : chr "Rural" "Rural" "Rural" "Rural" ...
## $ Sex : chr "Male" "Female" "Female" "Male" ...
## $ Age : int 38 40 20 19 18 35 29 14 13 6 ...
## $ MaritalST : Factor w/ 6 levels "Partner","Married",..: 2 2 5 5 5 2 2 5 5 NA ...
## $ Income : num 555 555 555 555 555 ...
## $ Expenditure: num 488 488 488 488 488 ...
## $ Employment : Factor w/ 3 levels "Unemployed","Inactive",..: 3 3 2 3 2 3 3 NA NA NA ...
## $ Poverty : Factor w/ 3 levels "NotPoor","Extreme",..: 1 1 1 1 1 3 3 3 3 3 ...
As seen in the previous output, the str function makes it possible to identify the class of each variable included in the database. For example, the variable HHID is of character type, as is the variable Sex, whereas the variables Income and Expenditure are numeric. The other attributes associated with the variables can also be reviewed directly in the code output. This function is especially useful for obtaining an overview of the content and structure of a database.