##------------------------------------------------------------------------## ## Script for Soc 740 Lecture 1 ## ## John Fox ## ## Winter 2014 ## ##------------------------------------------------------------------------## # R Basics # arithmetic, interacting with the interpreter 2 + 3 2 - 3 2*3 2/3 2^3 4^2 - 3*2 (4^2) - (3*2) 1 - 6 + 4 2^-3 -2--3 -2 - -3 # functions, obtaining help sqrt(4) # square-root log(100) # logarithm log(100, base=10) log(100, b=10) # abbreviation args(log) # function argument help(log) # documentation ?log # same as above example(log) # examples in help page help.search("log likelihood") # search all installed packages ??"log likelihood" # same as above RSiteSearch("log likelihood") log(100, 10) # by position `+`(2,3) # even operators are functions # vectors c(1, 2, 3, 4) # combine 1:4 # sequence operator 4:1 -1:2 seq(1,4) # sequence function seq(2, 8, by=2) seq(0, 1, by=.1) seq(0, 1, length=11) c(1, 2, 3, 4)/2 # vectorized arithmetic c(1, 2, 3, 4)/c(4, 3, 2, 1) log(c(0.1,1,10,100), 10) c(1, 2, 3, 4) + c(4, 3) # recycling c(1, 2, 3, 4) + c(4, 3, 2) # warning! # variables x <- 1:4 # assignment x x = 1:4 # equivalent, to be avoided x x/2 y <- sqrt(x) y x <- rnorm(100) # destroys original x x summary(x) # a 'generic' function x[21] # indexing x[11:20] x[-(11:100)] # drop elements # user-defined functions mean(x) sum(x)/length(x) myMean <- function(x) sum(x)/length(x) # function definition myMean(x) myMean(y) myMean(1:100) x # global variable x untouched # cleaning up objects() remove(x, y, myMean) objects() # Duncan example # creating a data frame from data stored in a file Duncan <- read.table('D:/data/Duncan.txt', header=TRUE) # assumes location of data Duncan Duncan <- read.table(file.choose(), header=TRUE) # a variation summary(Duncan) # recall 'generic' function # attaching a data frame prestige # error! attach(Duncan) # make data available (there are better ways!) prestige # distributions and bivariate relationships hist(prestige) # histogram plot(income, education) # scatterplot identify(income, education, row.names(Duncan)) # point identification # to exit point identification: Windows - right-click; Mac or RStudio - Esc row.names(Duncan)[c(6, 16, 27)] # fitting a regression duncan.model <- lm(prestige ~ income + education) duncan.model # print model object summary(duncan.model) # again 'generic' function # using a data frame in an attached package search() detach(Duncan) search() library(car) Prestige View(Prestige) # data viewer scatterplot(prestige ~ income, data=Prestige) # attach not needed ?scatterplot scatterplot(income, prestige, span=0.7, labels=row.names(Prestige)) # error! with(Prestige, # avoiding attach scatterplot(income, prestige, span=0.7, labels=row.names(Prestige))) scatterplot(prestige ~ income, span=0.7, data=Prestige) # even better (try varying the span) library(Rcmdr) # graphical user interface, graphs don't play well with RStudio