Get startedGet started for free

Analyzing the data

1. Analyzing the data

Now that we understand the Markdown file elements, let's analyze the data and include the analysis in the report.

2. Loading packages

Let's filter the investment_services_projects data to view information about the projects in a specific country. First, we load the dplyr package.

3. Loading packages

The packages are loaded in the first code chunk in the Markdown file, since this is the first chunk that runs when the file is knit. Listing all necessary packages at the beginning of the file also keeps it organized and ensures that we aren't listing packages that we already loaded elsewhere in the report.

4. Filtering for projects in Indonesia

Let's add a new code chunk with two sets of three backticks and curly braces with the letter r. Within the code chunk, we add the dataset name, a pipe, and filter for country equals equals Indonesia. We can see that there are 38 projects from the 2012 to 2018 fiscal years. Let's use assignment and the arrow operator to call this indonesia_investment_projects.

5. Filtering for projects in Indonesia in 2012

Let's add another code chunk and filter for the projects from a specific year, 2012. We add to the existing filter and specify the date disclosed as greater than or equal to July 1st 2011, and less than or equal to June 30th 2012, keeping in mind that the fiscal year is defined as starting on July 1st of the previous year through June 30th of the year of interest. Using assignment, we call this indonesia_investment_projects_2012. We can see that there are 6 projects from the 2012 fiscal year.

6. Including code results in text

We can also reference code results in the text of the report. This way, if we update the analysis and the value of the object changes, the text will update automatically to reflect the object value, and won't need to be edited manually. Let's use indonesia_investment_projects_2012 to calculate the total investment amount for projects in the 2012 fiscal year. We add a pipe and the summarize function, list the name of the new column we're creating, sum_total_investment, and pass the total_investment column to the sum function. We include na-dot-rm equals TRUE within the sum function to exclude any values that do not have a total investment amount.

7. Including code results in text

Below the code chunk where we create the object, we reference the object with the letter r to specify the language and the object name, in backticks. When we knit the file, we see the total investment amount, 435 million dollars, appears in the text of our report.

8. Multiple code chunks

At this point, we've added a few different code chunks to the file. If we run into problems when knitting the file, or receive errors about the code, it will become more difficult to spot the errors and easily edit them as we continue to add more code chunks.

9. Naming code chunks

To solve this issue, we can name each code chunk in the document so that the code is easier to identify. Naming each code chunk is a way of tagging the code throughout the report, which will be helpful when we are looking for a specific section we want to edit. When naming code chunks, it's best to give the code a name that provides some insight into what the code in that section achieves. The code chunk name should be included within curly braces after the letter r. In this case, we'll name the chunks indonesia-investment-projects and indonesia-investment-projects-2012 to clarify that these are the sections where we identify all projects in Indonesia, and all projects in Indonesia in the 2012 fiscal year.

10. Let's practice!

Let's practice!