Prepare Data: Manage Datasets

Sort observations

Before we do any data manipulation, it’s a good idea to sort data according to a dataset’s key ID variables. The key ID variables refer to those by which each observation of a dataset is uniquely identified. Take the pwt7 dataset as an example. Its key ID variables are country and year, which uniquely identify each row in the dataset. So, we can sort pwt7 first by country and then by year within each country, both in ascending order. We can use the order() function, applied to all rows and all columns of pwt7. We need to leave a blank entry right after the comma inside the brackets to denote all columns.

Get hands-on with 1200+ tech skills courses.