Workflow Basics

Coding Basics

# you can use R for calculations
1/200*30
[1] 0.15
(59+73+2)/3
[1] 44.66667
sin(pi/2)
[1] 1
# You can create new objects with the <-
# format: object <- value
# Reads as object name gets value 
# you can use ALT and minus(-) to create tehe <-
x<- 3*4
y <- 4*5
x
[1] 12
y
[1] 20

What’s in a Name

Object names must start with a letter
It can contain numbers, letters, _, and .

this_is_really_a_long_name <- 2.5
this

Calling Functions

R functions are called like this:
function_name(arg1 = val1, arg2=val2, …)

seq(1,10)
 [1]  1  2  3  4  5  6  7  8  9 10

Quotation marks comes in pairs. If you forget the closing mark, R will tell you with [+]

y <- seq(1,10, length.out = 5)
y
[1]  1.00  3.25  5.50  7.75 10.00

Exercises

  1. Why does this code not work ?
    my_variable <- 10 my-varIable # Error in eval(expr, envir, enclose): #> object “my varIable’ not found)

  2. Tweak each of the following R commands so that they run correctly:

library(tidyverse)
ggplot(dota=mpg)+
  geom_point(mapping = aes(x=displ, y=hwy))
Error in structure(list(data = data, layers = list(), scales = scales_list(),  : 
  argument "data" is missing, with no default
library(tidyverse)
ggplot(data=filter(mpg, cyl==8))+
  geom_point(mapping = aes(x=displ, y=hwy))

  1. Press Alt-Shift-k. What happens? How can you get to the same place using the menus?

Answer: Pops up the keyboard shortcut help!

LS0tDQp0aXRsZTogIlIgZm9yIERhdGEgU2NpZW5jZUNoYXB0ZXIgMiINCm91dHB1dDogaHRtbF9ub3RlYm9vaw0KLS0tDQo8aDE+IFdvcmtmbG93IEJhc2ljcyA8L2gxPg0KPGgyPiBDb2RpbmcgQmFzaWNzIDwvaDI+DQoNCmBgYHtyfQ0KIyB5b3UgY2FuIHVzZSBSIGZvciBjYWxjdWxhdGlvbnMNCjEvMjAwKjMwDQooNTkrNzMrMikvMw0Kc2luKHBpLzIpDQpgYGANCg0KYGBge3J9DQojIFlvdSBjYW4gY3JlYXRlIG5ldyBvYmplY3RzIHdpdGggdGhlIDwtDQojIGZvcm1hdDogb2JqZWN0IDwtIHZhbHVlDQojIFJlYW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