4 Workflow:basics

library("tidyverse")

4.1 Coding basics

R can be used as a calculator.

4.1.1 Exercise

No exercise.

4.2 What’s in the name

i_am_snake_case

IAmCamelCase

i.am.period.case

i_am.MixCase

4.2.1 Exercise

No exercise.

4.3 Calling functions

Call functions using the syntax below:

function_name(arg1 = value1, arg2 = value2)

4.3.1 Exercise

No exercise.

4.4 Practice

4.4.1 Exercise

Q1. Why does the code does not work?

my_variable <- 10
my_varıable
## Error in eval(expr, envir, enclos): object 'my_varıable' not found

Ans1. The code does not work because the variable which is assigned the value 10 is my_variable whereas the variable being called is different. The R will throw an error if it cannot find the variable in the environment.

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

ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy))

fliter(mpg, cyl = 8)
## Error in fliter(mpg, cyl = 8): could not find function "fliter"
filter(diamond, carat > 3)
## Error in filter(diamond, carat > 3): object 'diamond' not found

Ans2. There are typos in the above code. Although, the first part of the code is correct.

ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy))

fliter should be filter and = should be ==

filter(mpg, cyl == 8)
## # A tibble: 70 x 11
##    manufacturer model     displ  year   cyl trans  drv     cty   hwy fl   
##    <chr>        <chr>     <dbl> <int> <int> <chr>  <chr> <int> <int> <chr>
##  1 audi         a6 quatt…  4.20  2008     8 auto(… 4        16    23 p    
##  2 chevrolet    c1500 su…  5.30  2008     8 auto(… r        14    20 r    
##  3 chevrolet    c1500 su…  5.30  2008     8 auto(… r        11    15 e    
##  4 chevrolet    c1500 su…  5.30  2008     8 auto(… r        14    20 r    
##  5 chevrolet    c1500 su…  5.70  1999     8 auto(… r        13    17 r    
##  6 chevrolet    c1500 su…  6.00  2008     8 auto(… r        12    17 r    
##  7 chevrolet    corvette   5.70  1999     8 manua… r        16    26 p    
##  8 chevrolet    corvette   5.70  1999     8 auto(… r        15    23 p    
##  9 chevrolet    corvette   6.20  2008     8 manua… r        16    26 p    
## 10 chevrolet    corvette   6.20  2008     8 auto(… r        15    25 p    
## # ... with 60 more rows, and 1 more variable: class <chr>

diamond should be diamonds

filter(diamonds, carat > 3)
## # A tibble: 32 x 10
##    carat cut     color clarity depth table price     x     y     z
##    <dbl> <ord>   <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
##  1  3.01 Premium I     I1       62.7  58.0  8040  9.10  8.97  5.67
##  2  3.11 Fair    J     I1       65.9  57.0  9823  9.15  9.02  5.98
##  3  3.01 Premium F     I1       62.2  56.0  9925  9.24  9.13  5.73
##  4  3.05 Premium E     I1       60.9  58.0 10453  9.26  9.25  5.66
##  5  3.02 Fair    I     I1       65.2  56.0 10577  9.11  9.02  5.91
##  6  3.01 Fair    H     I1       56.1  62.0 10761  9.54  9.38  5.31
##  7  3.65 Fair    H     I1       67.1  53.0 11668  9.53  9.48  6.38
##  8  3.24 Premium H     I1       62.1  58.0 12300  9.44  9.40  5.85
##  9  3.22 Ideal   I     I1       62.6  55.0 12545  9.49  9.42  5.92
## 10  3.50 Ideal   H     I1       62.8  57.0 12587  9.65  9.59  6.03
## # ... with 22 more rows

Q3. Press Alt + Shift + K. What happens? How can you get to the same place using the menus?

Ans3. It gives the keyboard shortcuts menu. The same can be achieved under Tools -> Keyboard shortcuts or Help -> Keyboard shortcuts.