R Part 3

The total length of the videos in this section is approximately 12 minutes, but you will also spend time running code while completing this section.

You can also view all the videos in this section at the YouTube playlist linked here.

Please download the code file:

Multiple plots

RPart3.1.MultiplePlots.mp4

Question 1: How do you know whether you should use boxplot(vec1, vec2, vec3) or boxplot(combinedvec~groups) to plot 3 boxplots on one set of axes?

Show answer

Examining the data and determining whether you have a vector for each subset or whether you have 2 vectors, one containing all the data, and one containing codes that assigns the data to a group. Also, you can manipulate the data between these two formats if you have a strong preference!

Output as an object

RPart3.2.Accessing Output.mp4

Question 2: How can you determine what is able to accessed in a saved output using the " $ " syntax?

Show answer

Use the names() command, with your object name in the parentheses. While you can type words that you think should be accessible portions of the output, it is likely not the most efficient way to find out what you can access. In addition, items may not be labeled as you expect (ex. p.value instead of p-value or p value).

Helpful commands

RPart3.3.Helpful commands (by, apply, which, if else).mp4

Question 3: Which of the following outputs would be obtained by the code: 

ifelse(c(1,2,3,4) < 3, "Y", "N")

Show answer

The first option. 1 and 2 are less than 3, so the console will print "Y" twice, and 3 and 4 are not less than 3, so the console will print "N" twice.

Saving, loading, writing ,reading

RPart3.4.Saving, Loading, Writing.mp4

Question 4: For which of the following commands should the file name specified end in a '.R' extension? Check all that apply.

Show answer

save and load. Saving and loading are used with R objects that you want to use later, while writing and reading are used with different file types (csv, pdf, jpeg, etc.) so that you can access your work outside of the program.

Here is one more idea that will be helpful as you move on. When you change a variable or make a new version of it, the best practice is to create is as a new column in your data set:

dataset$LogIncome<-log(dataset$income)

is much better than

LogIncome<-log(dataset$income)

The latter will not become a new column in your data set, and so it won't be included if you take a subset of your entire data set, reorder the rows, etc.

All done.

During this tutorial you learned:


Functions in review: 

boxplot(), groups(), by(), apply(), which(), if() { }, ifelse(), save(), load(), write.csv(), pdf(), dev.off()