----------------------------------- R INTRODUCTION PART II ----------------------------------- In this session, we will get introduced to R commands for inputting data and outputting R output and graphs. scan command data.entry command read.table command saving output saving graphs # There are several convenient ways of getting data into R. # INPUTTING DATA FROM THE KEYBOARD: # For small datasets, one can type the data directly into R using the scan command. # Just type mydata=scan() # and enter the numbers, one or more on a line, and type a blank line when you are # finished. # Here's an example, entering in the temperatures 45, 55, 65, 23, 33. I type # temps at the end to verify that the data has been entered in correctly. > temps=scan() 1: 45 2: 55 65 4: 23 5: 33 6: Read 5 items > temps [1] 45 55 65 23 33 # Another convenient way of entering data is by the data.entry command, where you # enter data by a spreadsheet format # First, you have to define the variable name that will hold the data. This indicates # that mydata contains one value that is missing (R uses NA for missing). mydata=c(NA) # Then you can type data.entry(mydata) # You'll see a spreadsheet window. You enter data into the spreadsheet (replace the NA # entry with a number) and close the window # when you are done. # READING IN SOME DATA INTO R FROM A FILE # For larger data files, I recommend creating them in some program outside of R # and then reading them into R using the read.table command. # Here is a simple example: I have the following data that I wish to enter into R: # (the number of wins and losses for five baseball teams) TEAM WINS LOSSES Kansas City 54 43 Minnesota 49 49 Chicago 49 50 Cleveland 41 58 Detroit 26 71 # STEP 1: I entered the data into Excel, the first row containing the variable names # TEAM, WINS, LOSSES. # STEP 2: I save the data as a "text, tab-delimited" file -- I call the file winslosses.txt # STEP 3: You have to place the file in a location where R can read it. R has a "working # directory" where it is looking for files. You can change the working directory # to the folder which contains the data file. # STEP 4: You read the data into R using the read.table command. Note that we indicate in # the command that there is a header line with the column names. data=read.table("winslosses.txt",header=T) # look at the data matrix data # SAVING R OUTPUT # Here we describe several ways of saving R output. # If you wish to save all or some of the output displayed in the R Console, then just # select and copy the output and paste it into your favoriate word processor like Word. # R also keeps track of all of the commands that you type in. To save these commands, # select Save History from the File menu and save this into a text file. # You can also save graphs that are created using R using standard Copy commands. We'll # mention this again when we discuss graphing on R. ########## END OF R SESSION ###################