15 Introduction to R

# 15 Introduction to R - University of California Los Angeles...

This preview shows pages 1–3. Sign up to view the full content.

University of California, Los Angeles Department of Statistics Statistics 100A Instructor: Nicolas Christou Data analysis with R - Some simple commands When you are in R , the command line begins with > To read data from a website use the following command: data <- read.table("http://www.stat.ucla.edu/~nchristo/statistics100A/ body_fat.txt", header=TRUE) The result of the command read.table is a “data frame” (it looks like a table). In our example we give the name data to our data frame. The columns of a data frame are variables. This ﬁle contains data on percentage of body fat determined by underwater weighing and various body circumference measurements for 251 men. Here is the variable description: Variable Description x 1 Density determined from underwater weighing x 2 Percent body fat from Siri’s (1956) equation x 3 Age (years) x 4 Weight (lbs) x 5 Height (inches) x 6 Neck circumference (cm) x 7 Chest circumference (cm) x 8 Abdomen 2 circumference (cm) x 9 Hip circumference (cm) x 10 Thigh circumference (cm) x 11 Knee circumference (cm) x 12 Ankle circumference (cm) x 13 Biceps (extended) circumference (cm) x 14 Forearm circumference (cm) x 15 Wrist circumference (cm) If the data ﬁle is on your computer (e.g. on your desktop), ﬁrst you need to change the working directory by clicking on Misc at the top of your screen and then read the data as follows: > data <- read.table("filename.txt", header=T) Note: the expression <- is an assignment operator. Once we read the data we can display them by simply typing at the command line < data . Or if we want we can display the ﬁrst 6 rows of the data by typing > head(data) . Here is the output: > head(data) x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 1 1.0853 6.1 22 173.25 72.25 38.5 93.6 83.0 98.7 58.7 37.3 23.4 30.5 28.9 18.2 2 1.0414 25.3 22 154.00 66.25 34.0 95.8 87.9 99.2 59.6 38.9 24.0 28.8 25.2 16.6 3 1.0754 10.3 23 188.15 77.50 38.0 96.6 85.3 102.5 59.1 37.6 23.2 31.8 29.7 18.3 4 1.0722 11.7 23 198.25 73.50 42.1 99.6 88.6 104.1 63.1 41.7 25.0 35.6 30.0 19.2 5 1.0708 12.3 23 154.25 67.75 36.2 93.1 85.2 94.5 59.0 37.3 21.9 32.0 27.4 17.1 6 1.0775 9.4 23 159.75 72.25 35.5 92.1 77.1 93.9 56.1 36.1 22.7 30.5 27.2 18.2 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Useful commands: Extracting one variable from the data frame (e.g. the second variable): > data[,2] Another way to extract a variable : > data \$ x2 Similarly if we want to access a particular row in our data (e.g. ﬁrst row): > data[1,] To list all the data simply type: > data To compute the mean of all the variables in the data set: > mean(data) To compute the mean of just one variable:
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/14/2011 for the course STATS 100A taught by Professor Wu during the Fall '07 term at UCLA.

### Page1 / 10

15 Introduction to R - University of California Los Angeles...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online