Introduction to R Lukáš Lehotský Courses' objectives • Introduction to basic concepts and techniques of data manipulation, qualitative comparative analysis and text analysis with R • Not a course on general methodology or research design • Not a course on programing in R, but includes programming introduction interlinked with data manipulation R: advantages • Extremely powerful • Freeware and open source • Integrated • Transparent/easy to find errors • Modular (re-usability of codes) • Fast compared to "point-and-click" programs R: disadvantages • Steep learning curve compared to "point-and-click" programs • Data preparation possibly demanding • Inconsistencies across packages • Package-dependent (some particular operations not implemented) • Slower compared to other programming languages in large operations R community / resources • R package / library manuals • R site: http://cran.r-project.org • Community forums • htt p: //sta c kove rf I o w. co m • http://www.statmethods.net • http://www.r-bloggers.com • Youtube videos https://www.voutube.com/watch?v=qHfSTRNg6i R: not a silver bullet • Some tasks are cumbersome in R • Other software much better in some particular tasks • Sophisticated scripting does not offset poor research design R: focus on logic • Any programming language is just very condensed and formalized speech • The most complicated part is designing the procedure of what needs to be done • Writing the actual script is fairly easy Introduction to R • You should have two programs installed on computer • R • R Studio • Both have to be installed to run R Studio • We are going to use R Studio • More convenient to work with RStudio Eile | Edit Code View Plots Session Build Debug Profile lools Help l " Addins Ol -I Chi - Console Terminal ^ Environment History Connections bJ Import Dataset - Global Environment - %\ Project: (None) ■ List - R version 3.4.1 (2917-66-39) -- "Single Candle" Copyright (C) 2917 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'licenseQ' or 'licenceQ' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demoO' for some demos, 'helpO' for on-line help, or 1 help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > Environment is empty File Plots Packages Help Viewer ■3 Export - RStudio _ □ £ile | Edit Code View Plots Session Build Debug Profile lools Help H " Addins £-1 Project: [None] " Console Terminal R version 3.4.1 (2917-66-39) -- "Single Candle" Copyright (C) 2917 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'licenseQ' or 'licenceQ' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demoO' for some demos, 'helpO' for on-line help, or 1 help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > Console Environment History Connections \2* fej P* Import Dataset -Global Environment - List - Environment is empty Environment History Files Plots Packages Help Viewer I tE| Export - Plots Packages Help Viewer Introduction to R: console vs. script • Console provides instant input • Short bits of code executed one by one • Very similar to other console programs (e.g. Linux) • Enter runs the command • Faster • Does not contain history • Scripts • Complete piece of code • Executed at once • Executed bit by bit • CTRL + SHIFT + N creates new script in R Studio • Ctrl + Enter runs the selected piece of code O RStudio £ile Edit Code View Plots Session Build Debug Profile lools Help O ' %| ^+ -I y £ji 9 | | Go to file/function I ED " Addins - £.1 Project: (None] - ^^ntitled^™ Environment History Connections 1 y □ Source on Save 0^ — I ^ 3* Run 1 3* 1 _3* Source - 1 Id 1 Import Dataset - { List- 1 1 Global Environment - Environment is empty Environment Scripting window (CTRL + SHIFT + N) History Files Plots Packages Help Viewer * # I uj aD 1 Q, 1 1 @ R: Documentation ~ Find in Topic help {utils} R Documentation A Documentation 1:1 ^TToc^vel^^^ ^^^crigt - Console Terminal Description PlotS You are welcome to redistribute it under certain conditions. Type 'licenseQ' or 'licenceQ' for distribution details. R is a collaborative project with many contributors. Type 'contributors{)' for more information and 'citation()' on how to cite R or R packages in publications. A help is the primary interface j^Decl^idf^GS Usage Help help(topic, package = HULL, lib.loc = NULL, verbose = getOptikrfjT^erl)»fa") , try. all. packages =* gSopntTOVL ("help . try. all. packag help type = getOption. ("help type")] Type 'demoO' for some demos, 'help{)' for on-line help, or ■ help.start<) ' for an HTML brows^cQPISOntee to help. Type 'q()' to quit R. > help() > | V Arguments topic usuallv. a name or character strina specifvina the topicfor which help is sought. A character string (enclosed in explicit single or double quotes) is always taken as naming a topic. < > V Object-oriented programming Object • object: instance of a certain data class that can be manipulated according set of procedures (methods) one <- 1 o RStudio - °D Eile | Edit Code View Plots Session Build Debug Profile lools Help CH Ä*l Q 01 Qli^Goto file/function 1 @ - Addins » Project: (None) » <3j Untitledl* x =n j Environment History = n 1 <£L 1 H □ Source onSave 1 one <- 1 > one + one [1] 2 > Files Plots Packages Help Vie Ler Install Update I* I I s Name Description j f Version User Library A □ assertthat Easy Pre and#ost Assertions 0.2.0 © □ audio Audio IntsJFace for R 0.1-5 e beepr Easilyjrey Notification Sounds on any ~->\amrn 1.2 © □ BH M (Boost C + + Header Files 1.62.0-1 © □ blndr ^^^^ Parametrized Active Bindings 0.1 © ^rloYcpp An 'Repp' Interface to Active Bindings 0.2 © □ bitops Bitwise Operations 1.0-6 □ Cairo R graphics device using cairo graphics library for creating high-quality bitmap [PNG, JPEG, TIFF), vector [PDF, SVG, PostScript) and display [X11 and Win32) output 1.5-9 chron Chronological Objects which can Handle Dates and Times 2.3-50 © □ colorspace Color Space Manipulation 1.3-2 © □ CL "1 A Modern and Flexible Web Client for R 23.1 © □ data.table Extension of'data,frame' 1.10.4 © n dichromat Color Schemes Jor Dichronats 2.0-0 Object: use • Any output of any operation needs to be stored • new object may be created • existing object may be rewritten one <- 1 one + one [11 2 two <- one + one two [11 2 o RStudio - °D File Edit Code View Plots Session Build Debug Profile lools Help CH Ä*l Q 01 Qli^Goto file/function 1 @ - Addins » ^ Project: (None) » <3j Untitledl* x aD Environment History = n 1 <£L 1 H □ Source onSave 1 one <- 1 > one + one [1] 2 > two <- one + one > Files Plots Packages Help Viewer Ol Install (£) Update i O Name Description I Version User Library A □ assertthat Easy Pre and Post Assertions m 0.2.0 □ audio Audio Interface for R m 0.1-5 © beepr Easily Play Notification Sounds m\ any Platform f 1.2 © □ BH Boost C+ + Header Files^r 1.62.0-1 © □ bindr Parametrized Actiyafcindings 0.1 © □ bindrcpp An 'RcppJ|()efface to Active Bindings 0.2 © □ . bitops ^^^^^^ *Frwise Operations 1.0-6 TT Cairo R graphics device using cairo graphics library for creating high-quality bitmap [PNG, JPEG, TIFF), vector [PDF, SVG, PostScript) and display [X11 and Win32) output 1.5-9 chron Chronological Objects which can Handle Dates and Times 2.3-50 © □ colorspace Color Space Manipulation 1.3-2 © □ CL "1 A Modern and Flexible Web Client for R 23.1 © □ data.table Extension of'data,frame' 1.10.4 © n dichromat Color Schemes Jor Dichronats 2.0-0 RStudio Eile Edit Code View Plots Session Build Debug Profile lools Help one <- 1 > one + one [1] 2 > two <- one + one > two [1] 2 > R Script : Environment History = n Q J* Import Dataset - List - I 10 [1] FALSE as.character(5 > 10) [1] "FALSE" Tips and tricks • Keyboard hacks • For Czech keyboard and Win machine users, Right Alt (AltGr) allows you to type some special characters (AltGr + < for <) • Script hacks • # allows you to write comments in scripts • When writing code, R will automatically add closing bracket, as well as closing quote symbol • Tab will finish the name of function or argument in R Studio - try typing help( and press Tab Practice • Create a new script • Define 5 objects of your own liking (including object with more than one element) • Get a class of each of the following items: 5, " 5 ", NA, TRUE, "true", NULL • Calculate the equation (5.5*4) + (7.5*2. 12) using R objects 12 7 • Try to get a square root of number 254 MY GOD ... THi& WILL KJErVJ A HftJ-DOZEhl fflPERS. f\ 7VlESft Of? TWO, AND A PARAGRAPH IN EVtRV TEXTBOOK OfJ QUEUING THEORY] Source: https://xkcd.com/664/