-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTutorial_2_script__Coding_in_R.R
102 lines (67 loc) · 3.79 KB
/
Tutorial_2_script__Coding_in_R.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Tutorial 2 - Coding in R
############################################################################################
## ##
## Common notation in R ##
## ##
############################################################################################
# R as a calculator
3+4 #addition
3-4 #subtraction
3*3 #multiplication
3/4 #division
# Assigning values to the objects x and y
x <- 5
y <- 2
# The output of these commands will print to the console
x+y #addition
x-y #subtraction
x*y #multiplication
x/y #division
x^y #exponential
# This command assigns the output of x+y to z
z <- x+y
# Print the value of z to the console
z
print(z) # this works too, but it takes longer to type
############################################################################################
## ##
## Setting up R ##
## ##
############################################################################################
# Set the working directory (the folder you want to work in)
setwd("/Users/your_username/folder/subfolder") # Note that if you run this line you will get an error as this directory/folder doesn't exist on your computer
# Examples for setting the working directory on Mac and PC:
setwd("/Users/aliciahadingham/Desktop") # a Mac example
setwd("C:/Users/alici/Desktop") # a PC example
# Print the current working directory to the console
getwd()
#List files in your current working directory
list.files()
# Install a R library/package for the first time on your computer
install.packages("ggplot2")
# Load a library which has already been installed
library("ggplot2") # library(ggplot2) also works
############################################################################################
## ##
## Getting help ##
## ##
############################################################################################
# How to get help on using specific commands/functions in R:
# * The help panel in RStudio (type in the command in the 'Help' panel)
# * Type ? followed by the command you want help with:
?setwd
?library
# How to get general help on R:
# * Google (search “How to.... in R”)
# * StackOverflow https://stackoverflow.com/questions (You can post questions here or find answers from previous questions)
############################################################################################
## ##
## Extra: Using quotations in R ##
## ##
############################################################################################
# You can use double quotations or single quotations in R
setwd("/Users/alici/Desktop") #Using double quotations
setwd('/Users/alici/Desktop') #Using single quotations
# But if you use 2 single quotations instead of double quotations you will get an error
setwd(''/Users/alici/Desktop'') #Using 2 single quotations
# Error: unexpected string constant in "setwd(''/Users/alici/Desktop''"