x c 1 2 3 4 y c 2 4 6 8 z c 10 20 w c 8 3 2 x y 1 2 2 4 3 6 4 8 1 2 8 18 32 x z

# X c 1 2 3 4 y c 2 4 6 8 z c 10 20 w c 8 3 2 x y 1 2 2

• 181
• 50% (6) 3 out of 6 people found this document helpful

This preview shows page 168 - 170 out of 181 pages.

x = c ( 1 , 2 , 3 , 4 ); y = c ( 2 , 4 , 6 , 8 ) z = c ( 10 , 20 ); w = c ( 8 , 3 , 2 ) x * y # 1 * 2, 2 * 4, 3 * 6, 4 * 8 [1] 2 8 18 32 x + z # 1+10, 2+20, 3+10, 4+20 [1] 11 22 13 24 y + w # oops [1] 10 7 8 16 Warning message: In y + w : longer object length is not a multiple of shorter object length Exercise 7: Why was y+w above the vector (10, 7, 8, 16) and why is there a warning? Solution: To get started, y+w = (2+8, 4+3, ...) ... The following commands are useful: ls () # list all objects " dummy " " mydata " " x " " y " " z " ls ( pattern = " my ") # list every object that contains " my " " dummy " " mydata " rm ( dummy ) # remove object " dummy " rm ( list = ls ()) # remove almost everything (use with caution) data () # list of available data sets help ( ls ) # specific help (?ls is the same) getwd () # get working directory setwd () # change working directory q () # end the session (keep reading) and a reference card may be found here: . When you quit, R will prompt you to save an image of your current workspace. Answering yes will save the work you have done so far, and load it when you next start R . We have never regretted selecting yes , but we have regretted answering no . If you want to keep your files separated for different projects , then having to set the working directory each time you run R is a pain. If you use RStudio , then you can easily create separate projects (from the menu File ): . Otherwise, there are easy work-arounds, but it depends on your OS. In Windows, copy the R or RStudio shortcut into the directory you want to use for your project. Right click on the shortcut icon, select Properties , and remove the text in the Start in: field; leave it blank and press OK . Then start R or RStudio from that shortcut.
R.4 Basics 169 Exercise 8: Create a directory that you will use for the course and use the tricks previously mentioned to make it your working directory (or use the default if you don’t care). Load astsa and use help to find out what’s in the data file cpg . Write cpg as text to your working directory. Solution: Assuming you started R in the working directory: library ( astsa ) help ( cpg ) # or ?cpg Median ... write ( cpg , file =" zzz . txt ", ncolumns = 1 ) # zzz so it ' s easy to find Exercise 9: Find the file zzz.txt previously created (leave it there for now). Solution: In RStudio , use the Files tab. Otherwise, go to your working directory: getwd () " C:\TimeSeries " Now find the file and look at it; there should be 29 numbers in one column. To create your own data set, you can make a data vector as follows: mydata = c ( 1 , 2 , 3 , 2 , 1 ) Now you have an object called mydata that contains five elements. R calls these objects vectors even though they have no dimensions (no rows, no columns); they do have order and length: mydata # display the data [1] 1 2 3 2 1 mydata [ 3 ] # the third element [1] 3 mydata [ 3 : 5 ] # elements three through five [1] 3 2 1 mydata [-( 1 : 2 )] # everything except the first two elements [1] 3 2 1 length ( mydata ) # number of elements [1] 5 dim ( mydata ) # no dimensions NULL mydata = as . matrix ( mydata ) # make it a matrix dim ( mydata ) # now it has dimensions [1] 5 1 If you have an external data set, you can use scan or read.table (or some variant) to input the data. For example, suppose you have an

#### You've reached the end of your free preview.

Want to read all 181 pages?

• Spring '08
• WU,KaHo
• The Land