There are different data types used in R. The type of a variable will for example determine what kind of operators that can be done on it. At the end of this exercise you should know:
From the lecture you might remember that all elements in any data structures found in R will be of a certain type. The four most commonly used data types in R are: logical, integer, double (often called numeric), and character. The names hints at what they are.
Bonus: Complex numbers have both a real and imaginary part (e.g. 3 + 4i); is a special number defined as the square root of –1., for example. We normally cannot take √(–1), so mathematicians introduced a new idea: of using i, in this example`. It is mostly used in engineering and physics.
In many cases the type of on entry is determined by the content so if you save the value 5.1 as a variable in R, the variable will automatically be recognised as numeric. If you instead have a text string like “hello world” it will have the type character. Below you will also see examples of how you can specify the type and not rely on R inferring the right type based on content.
In all exercises during this course, it is important that you try to figure out what the expected result would be, prior to running the commands. You should then verify that this will indeed be the result by running the command. In case there is a discrepancy between your expectations and the actual output make sure you understand why before you move forward. If you cannot figure out how to, or which command to run you can click the key to reveal example code including expected output. If you are trying out things on your own have a hard time understanding what is going on, ask the TAs or someone sitting next to you who might have wrapped their head around the issue.
Also note that in many cases there are multiple solutions that solve the problem equally well.
We do recommend to write all code in a Rmarkdown document in R-studio as that will at the end of the course be your own R tutorial with comments and code solutions.
Open Rstudio and make sure to set your working directory (either in console or in Rmd setwd("path/to/my/folder/"). Double check that you do not have stored objects in your current session with the following command. This will list all objects that you have in
your current R session.
ls()
## [1] "A" "address" "asst" "B"
## [5] "car.names" "char_vec" "cities" "cl"
## [9] "cn" "cn_vec" "cnames" "cntr"
## [13] "color_primary" "color_text" "cor_result" "crashes"
## [17] "df" "df1" "dfr" "doctor"
## [21] "drinks" "e" "E" "E.means"
## [25] "E.medians" "E.mm" "eng_output" "fa"
## [29] "fa1" "fa2" "gapminder" "i"
## [33] "l" "lego" "letnum" "linkoping"
## [37] "list.2" "list.a" "loc" "lund"
## [41] "mat1" "mt.merged" "mtcars2" "mylist"
## [45] "numeric_vec" "p" "p1" "p2"
## [49] "penguins" "r" "random1" "random2"
## [53] "s" "sel_cn" "sharks" "stackoverflow"
## [57] "tmp_cn" "tmp_count" "tmp_data" "tmp_df"
## [61] "tmp_max" "tmp_mean" "tmp_min" "tmp_missing"
## [65] "tmp_sd" "tmp_var" "umea" "uppsala"
## [69] "uppsala2" "vars" "vec1" "vec2"
## [73] "vector1" "vector2" "vector3" "videogames"
## [77] "X" "X.2"
In case you have objects that you want to remove from the current session you can do so with the rm() function. This command will remove all objects available in your current environment.
rm(list = ls())
This command uses commands that we have not talked about yet. If you do not understand how it works now, you will do so after tomorrows lectures and exercises.
var1 <- 11
var2 <- 34
var3 <- var1 + var2
var3
## [1] 45
pi in R)
class(var1)
typeof(var1)
## [1] "numeric"
## [1] "double"
class(pi)
typeof(pi)
## [1] "numeric"
## [1] "double"
text1 <- "test1"
text2 <- "test2"
class(text1)
typeof(text1)
## [1] "character"
## [1] "character"
Add var1 to it. What is the result and why?
text1+var1
## Error in text1 + var1: non-numeric argument to binary operator
as.integer(var3)
i <- 175
as.double(i)
as.double(text1)
## [1] 45
## [1] 175
## [1] NA
floor(pi)
ceiling(pi)
round(pi, digits=3)
## [1] 3
## [1] 4
## [1] 3.142
is.integer(floor(pi))
## [1] FALSE
"3.56437" string as number.
as.numeric('3.56437')
## [1] 3.56437
Inf/-Inf
## [1] NaN
TRUE * FALSE
T^7
## [1] 0
## [1] 1
grep check the help and learn how to use it by ?grep),
line1 <- "Hello darkness my old friend"
line2 <- "I've come to talk to you again"
paste(line1, line2, sep = "")
paste(line1, line2, sep = "*")
grep('and', line2)
sub('Hello', 'Goodbye', line1)
substr(line1, 5, 5 + 5)
## [1] "Hello darkness my old friendI've come to talk to you again"
## [1] "Hello darkness my old friend*I've come to talk to you again"
## integer(0)
## [1] "Goodbye darkness my old friend"
## [1] "o dark"
?function_name),vignette("cgmisc")),
Warning
cgmisc does not ship any vignettes.help(package="cgmisc"). What do you see?example(function, package="package_name")),genetics (search CRAN genetics),