• No results found

A note about producing figures and output

In document Statistics and Data With R (Page 55-60)

Nearly all the figures in this book were produced by the code that is explained in detail with the relevant examples. However, if you wish to produce the figures exactly as they are scaled here and save them in files to be included in other documents, you will need to do the following.

Use the function openg() before plotting. When done, call saveg(). To produce some of the histograms, use the function h() and to produce output with no quotes and no dimension names, use nqd(). These functions are explained next.

1.11.1 openg()

This function opens a graphics device in Windows. If you do not set the width and the height yourself, the window will be 3× 3 inches. Here is the code

openg <- function (width = 3, height = 3, pointsize = 8) {

windows(width = width, height = height, pointsize = pointsize)

}

and here are examples of how to use it

> openg() # 3in by 3in window with font point size set to 8

> openg(width = 4, height = 5, pointsize = 10)

> openg(4, 5, 10) # does the same as the above line

The second call draws in a window 4× 5 inches and font size of 10 point.

1.11.2 saveg()

This function saves the graphics device in common formats.

saveg <- function (fn, width = 3, height = 3, pointsize = 8) {

dev.copy(device = pdf, file = paste(fn, ".pdf", sep = ""), width = width, height = height,

pointsize = pointsize) dev.off()

dev.copy(device = win.metafile, file = paste(fn, ".wmf", sep = ""), width = width, height = height,

pointsize = pointsize) dev.off()

dev.copy(device = postscript, file = paste(fn, ".ps", sep = ""),

width = width, height = height, pointsize = pointsize)

dev.off() }

The first (and required) argument is fn, which stands for file name. The function saves the plotting window in PDF, WMF and PS formats. You can use these formats to import the graphics files into your documents. PDF is a format recognized by Adobe Acrobat, WMF by many Windows applications and PS by application that recognize postscript files. Here are a couple of examples of how to use saveg():

> saveg('a-plot')

> saveg('b-plot', 5, 4, 11)

The first statement saves the current graphics device in three different files, named a-plot.pdf, a-plot.wmf and a-plot.ps. The second statement saves three b-plot files, each with width of 5 in, height of 4 in and font size of 11 point. To avoid distortions in the graphics files, always use the same width, height and pointsize in both openg() and saveg().

1.11.3 h()

This function is a modification of hist(). Its effect is self-explanatory.

h <- function(x, xlab = '', ylab = 'density', main = '', ...){

hist(x, xlab = xlab, main = '', freq = FALSE, col = 'grey90', ylab = ylab, ...)

}

1.11.4 nqd()

This function prints data with no quotes and no dimension names.

nqd <- function(x){

print(noquote(no.dimnames(x))) }

Assignments 41

1.12 Assignments

Exercise 1.1. Answer briefly:

1. What is the difference between help() and help.search()?

2. Show an output example for each of these two functions.

3. List and explain the contents of each section in the help() window.

4. When you type the command help.search(correlation) you get an error mes-sage. Why?

5. How would you correct this error?

6. In response to help.search('variance') you get a window that shows two items:

var(base)and Var(car). Explain the difference between these. If you do not see these two items, explain why they do not show up.

7. When you type example(plot), you end up with a single plot. Yet, if you look at the Examples section in the help window for plot(), you will see that there is code that produces more than one plot. Why do you end up with only one plot?

Exercise 1.2.

1. Give the command that creates the sequence 0, 2, 4, . . . , 20.

2. Give the command that creates the sequence 1, 0.99, . . . , 0. Do not use the by argument. Use the length argument.

3. Create a sequence that includes the first 5 and last 5 of the English lower case letters. Use the symbol :, c() and length() in a single statement to create this sequence.

4. Do the same, but the last 5 letters should be in upper case.

Exercise 1.3. Answer the following briefly:

1. What prompt do you get following the statement seq(1 : 10, by?

2. Why?

3. How would you restart typing the statement above by getting out of the contin-uation prompt?

Exercise 1.4.

1. Create a file, named script.R. The file should include statements that plot 100 uniform random values of x against 100 uniform random values of y, both between 0 and 100. Attach a printout of the file to your answers.

2. Attach the plot you produced. The plot should be embedded in your favorite word processor.

3. Print the values of x and y that you created to two separate files, x.txt and y.txt. Attach a printout of these files with your answers.

4. Attach an unadulterated history file of the last 50 statements that you used in producing the answers to this exercise.

Exercise 1.5.

1. Create two projects in a root directory named book on your computer. Call one project ch2 and the other ch3.

2. In each project directory, save a .First() function in .RData. Set in .First() the prompt to 'ch1> ' and 'ch2> ' (without the single quotes).

3. Your answer should include instructions about how to accomplish these tasks.

Exercise 1.6.

1. How would you prove that the assignment x <- 1 produces a vector?

2. Will the following addition work? Why?

x <- c(1, '2', 3) ; y <- 5 x + y

3. If it did not work, how would you fix the code above such that x + y would work?

4. What is the mode and length of x in the statement x <- vector()?

5. How would you create a zero length vector of numeric mode?

6. Let x <- c(1 : 1000, by = 2). What is the value of x[1]2+∙ ∙ ∙ + x[n]2divided by the number of elements of x?

7. Let x <- 1 : 6 and y <- 1 : 3. What is the length of the vector x * y? What are the values of its elements? Why?

8. Let x <- 1 : 7 and y <- 1 : 2. What is the length of the vector x + y? What are the values of its elements? Why?

9. Will the assignment x <- c(4 < 5, 'a' < 'b') work? What do you get?

10. What are the return values of the following statements? Explain!

(a) 4 == 4 & 5 == 5 (b) 5 != 5 | 6 == 6 (c) 5 == 5 | 6 != 6 (d) x <- 5 & y <- 6

(e) x <- NA ; y <- 5 ; x == NA & y == 5

11. Even though it is not as terse, you should insist on using TRUE instead of T in expressions. Why?

12. Discuss the difference between the functions is.na() and is.nan().

13. How would you show that R treats Inf as a number?

14. What will be the modes of x under the following assignments? Explain.

(a) x <- c(TRUE, 'a') (b) x <- c(TRUE, 1) (c) x <- c('a', 1)

15. Give examples (with code) of how to subset vectors using the following index types:

(a) The index is a vector of logical values.

(b) The index is a vector of positive integers.

(c) The index is a vector of negative integers.

(d) The index is a vector of strings.

16. Explain the following result:

> x <- c(160, NA, 175, NA, 180)

> y <- c(NA, NA, 65, 80, 70)

> cbind(x = x[!is.na(x) & !is.na(y)], + y = y[!is.na(x) & !is.na(y)])

x y [1,] 175 65 [2,] 180 70

Assignments 43

Exercise 1.7.

1. Execute x <- letters in R. What did you get?

2. Execute x <- LETTERS in R. What did you get?

3. Use your discovery of letters and LETTERS to create a vector x of the first 10 lower case alphabet letters.

4. What is the mode of x?

5. What happens when you coerce x to logical?

6. What happens when you coerce x to numeric?

7. Now let x <- 0 : 10. What happens when you coerce x to logical?

Exercise 1.8.

1. What would be the results of the following statements? Why?

> x <- c(TRUE, TRUE, FALSE) ; y <- c(0, 0, 0)

> x & y

> x && y

2. Write a short script that uses a for loop to create a vector x of length 10. Each element of x must be a uniform random number between 0 and 1.

Exercise 1.9. Write a function that takes a vector x as input and returns √x + 1.

Show the code and the results of calling the function with the sequence −1 : 10. Call the R function sqrt() from within your function.

Exercise 1.10. Find the package to which the function cor.test() belongs. Run cor.test()on x <- runif(10) and y <- runif(10) and display the results. If you cannot find the package on your system, install it from the Web.

Exercise 1.11. Customize your environment so that no more than 60 characters per line are written on the console. Show the content of the appropriate file or function that accomplishes this task for every R project.

2

In document Statistics and Data With R (Page 55-60)