read_csv("https://tinyurl.com/cjkuecnc")
to the name gene
In R, there are (unfortunately) two assignment operators. They have subtly different meanings (more details later).
<-
requires that you type two characters but better captures spirit of assignmnet=
is easier to type but incorrectly suggests mathematical equalityx
and y
everywhere.Main.database.first.object.header.length
).?make.names
for the complete rules on what can be a name.There are different conventions for constructing compound names. Warning: disputes over the right way to do this can get heated.
Error: object 'A' not found
for
is a reserved word in R. (It is used in loop control.)?Reserved
for the complete rules.aes(x=EIF3L, y=VAPA)
=
value.n_max
tells read_csv()
to only read the first 10 rows of the dataset.Why does this code generate errors?
x
remains the same after sqrt(x)
)Let’s say I have these variables and I want to add 1 to all of them and save the result.
This does the trick but it’s a lot of copy-paste
c(
)
function, which is short for “combine”+
) automatically work with multi-element vector arguments.c(1,2,3)
by the corresponding element of c(4,5,6)
A particular class has two quizzes which are taken by the same three students. Thier scores are below:
[1] 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] 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
:
is a handy shortcut to create a vector that is a sequence of integers from the first number to the second number (inclusive).5:-5
[1] "a"
[1] "b" "c" "d"
[1] "c" "a"
[1] "a" "a" "a" "a" "a" "a" "d"
What does this code do?
length(x)
does, then think about what the output of length(x):1
should do, and then finally x[length(x):1]
[1] "spring" "summer" "fall" "winter"
[1] spring summer fall winter
Levels: spring summer fall winter
TRUE
or FALSE
Error in numbers + 2: non-numeric argument to binary operator
What types are each of the following vectors? Are they all fundamentally the same, or are they different?
Which of these lines of code will run and which will produce an error?
NA
list
is like an atomic vector, except the elements don’t have to be the same type of thinga_vector = c(1,2,4,5)
maybe_a_vector = c(1,2,"hello",5,TRUE)
maybe_a_vector # R converted all of these things to strings!
[1] "1" "2" "hello" "5" "TRUE"
df$col
syntax works for data frames.str()
to dig into nested lists and other complicated objectsList of 12
$ coefficients : Named num [1:11] 79.048 -2.063 8.204 0.439 -4.619 ...
..- attr(*, "names")= chr [1:11] "(Intercept)" "mpg" "cyl" "disp" ...
$ residuals : Named num [1:32] -38.68 -30.63 13.01 -15.75 -8.22 ...
..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
$ effects : Named num [1:32] -829.8 296.3 124.8 -19.6 90.3 ...
..- attr(*, "names")= chr [1:32] "(Intercept)" "mpg" "cyl" "disp" ...
$ rank : int 11
$ fitted.values: Named num [1:32] 149 141 80 126 183 ...
..- attr(*, "names")= chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
$ assign : int [1:11] 0 1 2 3 4 5 6 7 8 9 ...
$ qr :List of 5
..$ qr : num [1:32, 1:11] -5.657 0.177 0.177 0.177 0.177 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:32] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...
.. .. ..$ : chr [1:11] "(Intercept)" "mpg" "cyl" "disp" ...
.. ..- attr(*, "assign")= int [1:11] 0 1 2 3 4 5 6 7 8 9 ...
..$ qraux: num [1:11] 1.18 1.02 1.29 1.19 1.05 ...
..$ pivot: int [1:11] 1 2 3 4 5 6 7 8 9 10 ...
..$ tol : num 1e-07
..$ rank : int 11
..- attr(*, "class")= chr "qr"
$ df.residual : int 21
$ xlevels : Named list()
$ call : language lm(formula = hp ~ ., data = mtcars)
$ terms :Classes 'terms', 'formula' language hp ~ mpg + cyl + disp + drat + wt + qsec + vs + am + gear + carb
.. ..- attr(*, "variables")= language list(hp, mpg, cyl, disp, drat, wt, qsec, vs, am, gear, carb)
.. ..- attr(*, "factors")= int [1:11, 1:10] 0 1 0 0 0 0 0 0 0 0 ...
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:11] "hp" "mpg" "cyl" "disp" ...
.. .. .. ..$ : chr [1:10] "mpg" "cyl" "disp" "drat" ...
.. ..- attr(*, "term.labels")= chr [1:10] "mpg" "cyl" "disp" "drat" ...
.. ..- attr(*, "order")= int [1:10] 1 1 1 1 1 1 1 1 1 1
.. ..- attr(*, "intercept")= int 1
.. ..- attr(*, "response")= int 1
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(hp, mpg, cyl, disp, drat, wt, qsec, vs, am, gear, carb)
.. ..- attr(*, "dataClasses")= Named chr [1:11] "numeric" "numeric" "numeric" "numeric" ...
.. .. ..- attr(*, "names")= chr [1:11] "hp" "mpg" "cyl" "disp" ...
$ model :'data.frame': 32 obs. of 11 variables:
..$ hp : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
..$ mpg : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
..$ cyl : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
..$ disp: num [1:32] 160 160 108 258 360 ...
..$ drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
..$ wt : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
..$ qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
..$ vs : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
..$ am : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
..$ gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
..$ carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
..- attr(*, "terms")=Classes 'terms', 'formula' language hp ~ mpg + cyl + disp + drat + wt + qsec + vs + am + gear + carb
.. .. ..- attr(*, "variables")= language list(hp, mpg, cyl, disp, drat, wt, qsec, vs, am, gear, carb)
.. .. ..- attr(*, "factors")= int [1:11, 1:10] 0 1 0 0 0 0 0 0 0 0 ...
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:11] "hp" "mpg" "cyl" "disp" ...
.. .. .. .. ..$ : chr [1:10] "mpg" "cyl" "disp" "drat" ...
.. .. ..- attr(*, "term.labels")= chr [1:10] "mpg" "cyl" "disp" "drat" ...
.. .. ..- attr(*, "order")= int [1:10] 1 1 1 1 1 1 1 1 1 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(hp, mpg, cyl, disp, drat, wt, qsec, vs, am, gear, carb)
.. .. ..- attr(*, "dataClasses")= Named chr [1:11] "numeric" "numeric" "numeric" "numeric" ...
.. .. .. ..- attr(*, "names")= chr [1:11] "hp" "mpg" "cyl" "disp" ...
- attr(*, "class")= chr "lm"
tibble()
to make your own data frames from scratch in Rdim()
gives the dimensions of the data frame. ncol()
and nrow()
give you the number of columns and the number of rows, respectively.names()
gives you the names of the columns (a vector)glimpse()
shows you a lot of information, head()
returns the first n
rowsmy_data.csv
(or whatever you chose to name it) appear in the specified location on your computer (e.g. Desktop
).csv
files in lots of programs (e.g. google sheets)1:10^2
Command-RETURN
(Mac), or Ctrl-ENTER
(Windows).cmd-<arrow>
and alt-<arrow>
for moving the cursor (by line and by word)cmd-shift-<arrow>
and alt-shift-<arrow>
for selecting text (by line and by word)alt
and drag your mouse up and downfind and add next
Write an R script that starts with:
In the rest of the script, do the following:
x
y
by computing the formula \(y = Ax^2 + Bx + C\) elementwisex
and y
as columnsx
vs y
Run your script to see the generated plot. Try changing the values of A
, B
, and C
at the top of the script and re-running to see how the plot changes.
Your result should look like:
Comments
#
to start a comment.