Docsity

cleaning data in r language, Appunti di Economia

Toggle sidebar
bg1
Cleaning data in R :
Intro to cleaning data in R:
Exploring Raw data :
•Understand the structure of your data"
•Look at your data"
•Visualize your data"
The first thing to do when you get your hands on a new dataset is to understand
its structure. There are several ways to go about this in R, each of which may
reveal different issues with your data that require attention."
In this course, we are only concerned with data that can be expressed in table
format (i.e. two dimensions, rows and columns). As you may recall from earlier
courses, tables in R often have the type data.frame. You can check the class of
any object in R with the class() function."
Once you know that you are dealing with tabular data, you may also want to get a
quick feel for the contents of your data. Before printing the entire dataset to the
console, it's probably worth knowing how many rows and columns there are. The
dim() command tells you this."
In addition to the class and dimensions of your entire dataset, str() will tell you the
class of each variable and give you a preview of its contents."
pf3
pf4
pf5

Anteprima parziale del testo

Scarica cleaning data in r language e più Appunti in PDF di Economia solo su Docsity!

Cleaning data in R :

Intro to cleaning data in R:

Exploring Raw data :

  • Understand the structure of your data
  • Look at your data
  • Visualize your data The first thing to do when you get your hands on a new dataset is to understand its structure. There are several ways to go about this in R, each of which may reveal different issues with your data that require attention. In this course, we are only concerned with data that can be expressed in table format (i.e. two dimensions, rows and columns). As you may recall from earlier

courses, tables in R often have the type data.frame. You can check the class of

any object in R with the class() function.

Once you know that you are dealing with tabular data, you may also want to get a quick feel for the contents of your data. Before printing the entire dataset to the console, it's probably worth knowing how many rows and columns there are. The

dim() command tells you this.

In addition to the class and dimensions of your entire dataset , str() will tell you the

class of each variable and give you a preview of its contents.

Although we won't go into detail on the dplyr package in this lesson (see the Data

Manipulation in R with dplyr course), the glimpse() function from dplyr is a

slightly cleaner alternative to str(). str() and glimpse() give you a preview of your

data, which may reveal issues with the way columns are labelled, how variables are encoded, etc.

You can use the summary() command to get a better feel for how your data are

distributed, which may reveal unusual or extreme values, unexpected missing data, etc. For numeric variables, this means looking at means, quartiles (including the median), and extreme values. For character or factor variables, you may be curious about the number of times each value appears in the data (i.e. counts),

which summary() also reveals.

Exploring Raw data 2 :

Introduction to tidy data :

Introduction to tidyr :

Introduction to tidyr 2 :

The separate() function allows you to separate one column into multiple columns.

Unless you tell it otherwise, it will attempt to separate on any character that is not

a letter or number. You can also specify a specific separator using the sep

argument.

The opposite of separate() is unite(), which takes multiple columns and pastes

them together. By default, the contents of the columns will be separated by

underscores in the new column, but this behavior can be altered via the sep

argument.

Type conversions :

Dates can be a challenge to work with in any programming language, but thanks to the lubridate package, working with dates in R isn't so bad. Since this course is about cleaning data, we only cover the most basic functions from lubridate to help us standardize the format of dates and times in our data.

As you saw in the video, these functions combine the letters y, m, d, h, m, s,

which stand for year, month, day, hour, minute, and second, respectively. The order of the letters in the function should match the order of the date/time you are attempting to read in, although not all combinations are valid. Notice that the functions are "smart" in that they are capable of parsing multiple formats.

String Manipulation :

  • str_trim() - Trim leading and trailing white space
  • str_pad() - Pad with additional characters
  • str_detect() - Detect a pattern
  • str_replace() - Find and replace a pattern

missing values from your data. For more information on why this is not always the best strategy, search online for "missing not at random.”

Outliers and obvious errors :

When dealing with strange values in your data, you often must decide whether they are just extreme or actually erroneous. Extreme values show up all over the place, but you, the data analyst, must figure out when they are plausible and when they are not.

Put all together :