Using R - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Using R

Description:

R as a statistical calculator. Creating data. Graphing and plotting. Statistical distributions. Dataframes. Summarising data. Using R ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 17
Provided by: harry66
Category:

less

Transcript and Presenter's Notes

Title: Using R


1
Using R
  • Harry R. Erwin, PhD
  • School of Computing and Technology
  • University of Sunderland

2
Resources
  • Crawley, MJ (2005) Statistics An Introduction
    Using R. Wiley.
  • Gonick, L., and Woollcott Smith (1993) A Cartoon
    Guide to Statistics. HarperResource (for fun).

3
Lecture Outline
  • R as a statistical calculator
  • Creating data
  • Graphing and plotting
  • Statistical distributions
  • Dataframes
  • Summarising data

4
Using R
  • We will work through a few examples of
    statistical calculations and creating data.
  • ylt-c(3,7,9,11)
  • zlt-scan()
  • alt-16
  • blt-seq(0.5,0.0,-0.1)
  • rep(value,count) creates a vector with value
    count times.
  • gl(upTo,repeats) can be used to generate factor
    data

5
Graphics
  • Examples of plot()
  • ?par for help on graphics parameters

6
Working with Dataframes
  • R works with data in dataframes, objects with
    rows and columns.
  • Each row is an observation or a measurement
  • Each column contain the values of a variable.
  • Variable types include numbers, text (factors),
    dates, or logical variables.
  • Columns have names. Rows have row.names.

7
Reading a Dataframe
  • wormslt-read.table("worms.txt", header T,
    row.names 1)
  • attach(worms)
  • names(worms)
  • If the row.names or the names are bad, you can
    set them to values.
  • worms
  • summary(worms)

8
Selecting Rows or Columns
  • worms,13 for all the rows and columns 1-3.
  • worms515, for the middle rows
  • wormsAreagt3 Slope lt 3, for logical tests
  • To sort a dataframe, you have to designate the
    columns to be sorted and the column to base the
    sort on wormsorder(worms,1),16
  • Example of a reverse sort

9
Vectors
  • ylt-c(5,7,7,8,2,5,6,6,7,5,8,3,4)
  • zlt-131
  • Try mean, var, range, max, min, summary, IQR,
    fivenum
  • y3
  • y37
  • yc(3,5,6,9)
  • y-1
  • y-length(y)
  • yygt6
  • zygt6
  • yy3!0

10
Vector Operations
  • is vector multiplication
  • If they are not the same length, the shorter
    vector is repeated as needed.
  • To join vectors, use the c() function
  • ?c
  • Subscripting can be based on a number, vector, or
    test.
  • To drop an element, subscript with a minus sign
    in front
  • Vectors can be combined with cbind() and rbind()

11
Arrays, etc.
  • Like vectors or dataframes with multiple
    dimensions
  • Lists can be used to combine data of different
    types.
  • val lt- list(varnamevalue,)
  • Although vectors are subscripted using , lists
    are subscripted with
  • Factors are special
  • citizen lt- factor(c("US","US","UK))
  • Examples from book.

12
Sorting and Ordering
  • Never sort a dataframe column on its own. The
    other columns are not sorted.
  • So dont use sort()
  • Instead use order(), since it leaves the
    dataframe unmodified. It returns a vector of
    subscripts, not values, but then you can apply
    the dataframe to the reordered vector to show it
    in the new order.

13
Table
  • Suppose vals is a collection of vectors
  • table(vals) reports the count of each unique
    value
  • tapply takes three arguments
  • Variable or dataframe to be summarised
  • Variable by which the summary is classified
  • Function to apply
  • Examples

14
Data Manipulation
  • To convert a continuous variable into a
    categorical variable, use cut(vals,levels)
  • You can also specify the break points
  • split() can be used to generate a list of vectors
    on the basis of the levels of a factor.
  • Example

15
Saving your Work
  • history(Inf)
  • savehistory("filename")
  • save(listls(), file "filename")
  • Tidying up
  • rm(var) any temporary variables
  • detach(dataframes)
  • rm(listls()) will clean up everything

16
Conclusions
  • There are other tools and languages
  • Minitab
  • SAS
  • Spreadsheets
  • Use what youre comfortable with.
  • But professional statisticians use R.
Write a Comment
User Comments (0)
About PowerShow.com