1. Use R Console

In R Console, at prompt >, type each of the following lines, and then hit the return key. 3+5 24 3/7 a=4 a 2a+10

2. Use R Editor

Go to File-New script. R Editor opens up. Type the above scripts. To execute, use curser to highlight one line or a block of scripts, go to Edit-Run line or selection (or right click). This method gives your much control on which script lines to run. For complex scripts involving many lines, running one line of script at a time allows you to check for errors. For script that you know works, you can run them in a block.

3. Two Special Notations

“#” Any words placed after this notation will not be executed. This allows you to write notes after it.

“<-” This notation assigns a value or function, works as =. But where you have to use =, you cannot use <-.

4. Some Basic Operations

getwd() # Check current work directory

setwd(“L:/workfile/R”) # To set work directory to L:/workfile/R

rm(list=ls()) # To remove all existing values and variables from the R memory

rm(a) # Only want to remove variable a

help(barplot) # get help on barplot

5. Bring a CSV File into R and Save a CSV File

data<-read.csv(file.choose()) # choose directory and name the file “data”

data<-read.csv(“C:/Myfolder/STATES.csv”, header=T) # Another way to bring data into R

fix(data) # display the file named “data”

names(data) # display fields or variables in the data

write.csv(data, file.choose()) # To save a csv file.

6. Extract Variables from a Dataset

pop90 = data$POP1990 # name variable POP1990 in data as pop90

area = data$AREA # name variable AREA in data as area

pop_density <- pop90/area

pop_density # show the result

pop1997 <- data$POP1997

pop1990 <- data$POP1997

PGR <- (pop1997-pop1990)/pop1990*100

PGR # show the result

7. Descriptive statistics

mean(pop90)

median(data$POPDEN90)

sum(data$POPDEN90)

8. Save the Script

Go to File-Save As.

To open saved script, go to File-Open script.