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
![](https://www.google.com/images/icons/product/drive-32.png)
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
![](https://www.google.com/images/icons/product/drive-32.png)
Question 2: How can you determine what is able to accessed in a saved output using the " $ " syntax?
Type words that you think should be reasonable, and see if you get an error or not.
Use the names() command, with your object name in the parentheses
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
![](https://www.google.com/images/icons/product/drive-32.png)
Question 3: Which of the following outputs would be obtained by the code:
ifelse(c(1,2,3,4) < 3, "Y", "N")
"Y" "Y" "N" "N"
"N" "N" "Y" "Y"
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
![](https://www.google.com/images/icons/product/drive-32.png)
Question 4: For which of the following commands should the file name specified end in a '.R' extension? Check all that apply.
save()
write()
load()
read()
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:
To create a set of boxplots visualizing several vectors and change the labels with names= argument
Review of the $ symbol and how to use it for objects
About several helpful functions for data manipulation
How to import and export csv files
How to save and load data and R objects as .R files
How to save graphics as a PDF or jpeg file
Functions in review:
boxplot(), groups(), by(), apply(), which(), if() { }, ifelse(), save(), load(), write.csv(), pdf(), dev.off()