The Big R-Book. Philippe J. S. De Brouwer

Чтение книги онлайн.

Читать онлайн книгу The Big R-Book - Philippe J. S. De Brouwer страница 25

The Big R-Book - Philippe J. S. De Brouwer

Скачать книгу

development environment

      image Hint – RStudio is free

      Rstudio can be downloaded from https://www.rstudio.com for free.

      RStudio is also in the repositories of most Linux distributions. That means that there is no need to go to the website or RStudio, but it is possible to download R, and install it with just one line in the CLI of your OS. For example, in Debian and its derivatives, this would be:

      # Note that the first 2 lines are just to make sure that # you are using the latest version of the repository. sudo apt-get update sudo apt-get upgrade # Install RStudio: sudo apt-get install rstudio

      An important note is that RStudio will work with the latest version of R that is installed. Also when you install MRAN, RStudio will automatically load this new version.

      Basic arithmetic

      The basic operators work as one would expect. Simply type in the R terminal 2+3 followed by ENTER and R will immediately display the result.

       addition product power

      #addition 2 + 3 #product 2 * 3 #power 2**3 23 #logic 2 < 3 x <- c(1,3,4,3) x.mean <- mean(x) x.mean y <- c(2,3,5,1) x+y

      image Hint – White space

      Usually, white space is not important for R.

      For example, mean ( c (c + pi, 2 ) ) will do exactly the same as mean(c(1+pi,2)). However, it is a good habit to add a white space before and after operators and after the comma. This will improve readability, which makes the debugging process easier and is helpful for other people who want to read your code.

      Editing variables interactively

      To create a variable x, type:

       scan()

      x <- scan()

      will start an interface that invites you to type all values of the vector one by one.

      In order to get back to the command prompt: type enter without typing a number (ie. leave one empty to end).

      image Further information – Other ways to import data

      This is only one of the many ways to get data into R. Most probably you will use a mix of defining variables in the code and reading in data from files. See for example Chapter 15Connecting R to an SQL Database” on page 327.

      edit(x)

      image Warning – Using CLI tools

      The edit function will open the editor that is defined in the options. While in RStudio, this is a specially designed pop-up window with buttons to save and cancel, in the command line interface (CLI) this might be vi. The heydays of this fantastic editor are over and you might never have seen it before. It is not really possible to use vi without reading the manual (e.g. via the man vi command on the OS CLI or an online tutorial). To get out of vi, type: [ESC]:q![ENTER]. Note that we show the name of a key within square brackets and that all the other strings are just one keystroke each.

      Batch mode

      R is an interpreted language and while the usual interaction is typing commands and getting the reply appear on the screen, it is also possible to use R in batch mode.

       batch mode

       functions

      1 create a file test.R

      2 add the content print(“Hello World”)

      3 run the command line Rscript test.R

      4 now, open R and run the command source(“test.R”)

       source()

      1 add in the file

      my_function <- function(a,b) { a + b }

      1 now repeat step 4 and run my_function(4,5)

      In this section we will present you with a practical introduction to R, it is not a formal introduction. If you would like to learn more about the foundations, then we recommend the documentation provided by the R-core team here: https://cran.r-project.org/doc/manuals/r-release/R-lang.pdf.

       variables

      In R, variables

       can contain letters as well as “_” (underscore) and “.” (dot), and

       variables must start with a letter (that can be preceded with a dot).

      For example, my_var.1 and my.Cvar are valid variables, but _myVar, my%var and 1.var are not acceptable.

      Assignment

      Assignment can be made left or right:

       assignment

      x.1 <- 5 x.1 + 3 -> .x print(.x) ## [1] 8

Скачать книгу