Chris Knight Beginners’ workshop
What is R? Statistics package General modelling tool Data visualisation and analysis tool Programming language Glorified pocket calculator “Environment for statistical computing and graphics”
Why use R? Lots of help available Free Big user base Increasingly used for doing science Jobs! Cutting edge and growing functionality If you learn only one new piece of computing software after Excel, learn R
What’s in this workshop Basic interaction with R Where to turn for help Moving rapidly to more complex analyses Where to learn more
What’s not in this workshop Advanced programming See e.g. ?Control, ?"function" and resources (later) ‘How do I analyse X?’ Google (e.g. ‘time series R’) and resources (later) The fundamentals of statistics See resources given later, esp. Crawley books Working in R environments other than Rstudio ( Default Windows GUIDefault Mac GUI R commander Command line
R as a calculator > [1] 10 > 5*0.5 [1] 2.5 > 27/3 [1] 9 > 9^2 [1] 81 > 9^(1/2) [1] 3 > sqrt(9) [1] 3 > log(5) [1]
R help documentation > log(5) [1] Check what the log() function is doing (e.g. which base?) > ?log General information Package Arguments and defaults What those arguments need to be What’s covered
> log(5) [1] Check what the log() function is doing (e.g. which base?) > ?log If you really care Where it’s come from Sometimes very useful, sometimes not Run from here or use example(log) What you get back Useful link R help documentation
> log(5) [1] Check what the log() function is doing (e.g. which base?) > ?log
R as a calculator > log(5) [1] Check what the log() function is doing (e.g. which base?) > ?log > log(5, base=10) [1] > log10(5) [1] What happens if you get it wrong? > log("Fred") Error in log(“Fred") : non-numeric argument to mathematical function > log function (x, base = exp(1)).Primitive("log") So if natural log isn’t ln(5), what is ln used for? > ?ln
R as a calculator Comparisons > 54 > 45 Greater than [1] TRUE > 2 < 1 Less than [1] FALSE > 15 == 15 Exactly equal [1] TRUE > 50 >= 50 Greater than or equal [1] TRUE > 5 > 1 & 9 <= 15 Are both statements true? [1] TRUE > 24 == 3 | 1 == 1 Is either statement true? [1] TRUE > ?Comparison > ?Syntax
Keeping track of what you’re doing Store code in a text file (.R) Write in comments #like this Run commands a line at a time Ctl R (Windows) ⌘↵ (Mac) Highlight code and run in the same way Run a whole file using the source() function Navigate history using arrows
Keeping track of what you’re doing Try running the first 32 lines one at a time or in one go url.show(“ s.Knight/docs/RbeginnersWorkshop14.R”) Try saving the file somewhere useful Try writing some more commented and uncommented lines Use R as a web browser to see the file containing this workshop’s code: Copy it to a new R script file (.R)
Further sections Assign values to a variable<- Create vectorsc() Simple statistics Extracting bits of variables [] loopfor (var in seq){} Data framesread.table(); write.table(); [,] Simple plotsplot(y~x); pairs(), hist() Build linear modelslm(y~x); anova() Modelling exerciserpart(y~x); randomForest(y~x)
Statistics Crawley Books Courses Masters’ lectures now! Places to go for more information Task views Specific books Many online with UoM access e.g. 43 in UseR! series: Data manipulation with R by Phil Spector very broadly useful R programming R site documents (start with ‘Documents with fewer than 100 pages’) Courses
Places to go for help R help list archives > Mailing lists > R-help web interface > Searchable archives R help documentation ? ?? help(package="base") demo() example("function") vignette() R support groups FLS (FLSRGROUP listerv) or Manchester R User group Google