Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basics of R, Ch Functions Help Managing your Objects

Similar presentations


Presentation on theme: "Basics of R, Ch Functions Help Managing your Objects"— Presentation transcript:

1 Basics of R, Ch 2.6-6 Functions Help Managing your Objects
Getting Data into R Getting Results out of R © Fall 2004 Don Edwards and the University of South Carolina

2 2.6 Functions Input arguments; output a new object (often a list)
Required and optional arguments Built-in R functions Output can depend on the object type User-designed functions Often developed iteratively Functions are intimidating to write, but you can monitor progress as you build them sequentially. Optional arguments can be skipped—order matters! The output can be a list, though it is often just a single numerical vector.

3 3 Help help, help.search, ?, ??,apropos
Generates extensive help file with several standard headings Some of most relevant info is hard to find (e.g., plot()) Examples section is often disappointing You have to “guess” at the right name. Help file includes Description, Value, Details, Examples, References. Relevant info for plot() is overwhelming. Examples are often arcane.

4 4 Managing Your Objects search() ls()
attach() and detach() alternately place and remove objects from search path attach() and source() are useful for reading files and inputting functions Search() shows directories and order in which R searches them. Attach() puts an object in its own directory. Though attach and source are useful for inputting functions too, though I often copy and paste.

5 4 Managing Your Objects (cont)
rm() to manage objects Saving a workspace (.Rdata file) Work with R in a specified (non-default) workspace by attaching the workspace Use pos=2 or greater to attach a read-only workspace Managing objects gets complex You can attach a workspace to (or just double-click) to add it to the current workspace. It’s easy (and dangerous) to have objects in multiple directories.

6 5 Getting Data into R 5.1 Creating Data
c(), seq(), rep() rnorm(), etc Creating matrices with matrix() Excel files are much easier to import and export than before (see read.xlsx and write.xlsx in xlsx package) The first bullet creates objects “by hand”. rnorm() creates data by simulation.

7 5.2 The read.table() Function
Standardized text data form brainbod example as.is=T option Missing values: NA Note first row with “missing” column name. as.is can be used to ensure character data is not converted to factor data. Can use na.strings=c() option.

8 5.2 The read.table() Function (cont)
No row names--header=T generates default integer row names No (column) names--a couple different options read.delim() and other variations appear to be more robust First bullet--First row will have correct number of names now too. Default column names are V1, V2,… Use names()= in R itself or col.names=c() in read.table.

9 5.3 The scan() Function A typical big-data-set setting
Thousands of records Multiple lines per record Variable-width fields Advanced feature scan() can also be used for the simplest types of data entry Y=scan() needs two carriage returns to terminate.

10 6. Getting Results Out of R
Not a strong point of R Outputting matrices, data frames t(), ncol Recent improvements (write.table, write.xlsx) round(), signif() Functions: fix() postscript and pdf commands save graphs and pictures Other simple methods involve menu selection and right-clicking Matrices and data frames are transposed and the number of columns will not be correct. write.table works correctly. For fixing functions, use Notepad window. Functions can be saved as a text file, but we usually work in the opposite direction (text file to function). Other methods for saving objects include right-clicking or menu-driven options.


Download ppt "Basics of R, Ch Functions Help Managing your Objects"

Similar presentations


Ads by Google