Overview
Teaching: 10 min
Exercises: 10 minQuestions
How can we convert a column from one data type to another?
How can we visualize relationships among columns?
Objectives
Transform a text column into a number column.
Identify and modify non-numeric values in a column using facets.
Lesson
Numbers
When a table is imported into OpenRefine, all columns are treated as having text values. We saw earlier how we can sort column values as numbers, but this does not change the cells in a column from text to numbers. Rather, this interprets the values as numbers for the purposes of sorting but keeps the underlying data type as is. We can, however, transform columns to other data types (e.g. number or date) using the Edit cells
> Common transforms
feature, as we did previously with the date
column. Here we will experiment with changing columns to numbers and see what additional capabilities that grants us.
Be sure to remove any Text filter
facets you have enabled from the left panel so that we can examine our whole dataset. You can remove an existing facet by clicking the x
in the upper left of that facet window.
To transform cells in the age
column to numbers, click the down arrow for that column, then Edit cells
> Common transforms…
> To number
. You will notice the age
values change from left-justified to right-justified, and from black to green in color.
Exercise 3.1
Try to transform the column
ENA-CHECKLIST
from text to numbers. Can all columns be converted to numbers?Solution
- Only observations that include only numerals (0-9) can be transformed into numbers. If you apply a number transformation to a column where the observations don’t meet this criterion, a yellow square will appear at the top starting with
Text transform on 0 cells in column [...]
. If you click theUndo / Redo
tab, you will see a step that starts with the same text, meaning that the data in that column was not transformed. Note that for some columns, some observations, but not all, can meet the criteria for being transformed into a number. This will result in a mix of transformed and non-transformed cells in the column.
Having several formats in the same column is not recommended.
Numeric facet
Sometimes there are non-number values or blanks in a column which may represent errors in data entry and we want to find them.
We can do that with a Numeric facet
.
Exercise 3.2
- For the column
age
you just transformed to numbers, edit one or two cells, replacing the numbers with text (such asabc
) or blank (no number nor text). You can hover over the cell and click the blueedit
that appears, and change theData type
totext
at the top in the scroll-down window.- Use the column pulldown menu to apply a
Numeric facet
to your edited column. The facet will appear in the left panel.- Notice that there are several checkboxes in this facet:
Numeric
,Non-numeric
,Blank
, andError
. Below these are counts of the number of cells in each category. You should see checks forNon-numeric
andBlank
if you changed some values.- Experiment with checking or unchecking these boxes to select subsets of your data.
When done examining the numeric data, remove this facet by clicking the x
in the upper left corner of its panel. Note that this does not undo the edits you made to the cells in this column. Use the Undo / Redo
function to reverse these changes.
Missing values
More than often we lack certain data for a variable. It may be an unknown origin of a sample, lacking information on age and sex, missing part of a date (only YYMM), or even encountering a value going to infinity. Regardless of origin we need to consider how we code them to avoid downstream analyses issues.
A rule of thumb is never to replace missing values with a zero (0). Even if a zero may be a representation of something not present, it is, per definition, not a missing value, but a zero value. Using zero for missing value may cause severe issues and should be avoided.
Different software may be equipped with different interpreters for missing values. For example, valid input in R include NULL
, NA
, NaN
, Inf
and -Inf
, while MatLab uses more context dependent options, e.g. missing
, or NaN
. The main point is that you need to consider how to code missing values in your data depending on what kind of software you plan on using. Also, for projects where more than one person is involved in managing the data, make sure you agree on a single format and stick to it.
Exercise 3.3
Find, identify and transform missing data in all columns of the dataset. What formats for missing data are appropriate for this dataset?
Solution
- The only missing values in the dataset are found in the columns
health state
,symptoms
anddisease outcome
. Since the values are not numerical a good choice could be to replace them with the optionNA
(Not available). Do this by usingedit
in a cell with a missing value, typeNA
and then selectApply To All Identical Cells
.