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

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

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

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

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

will use the arrow sign <- most often, however, R allows left assignment with the = sign.

      x.3 = 3.14 x.3 ## [1] 3.14

      There are also occasions that we must use the = operator. For example, when assigning values to named inputs for functions.

      v1 <- c(1,2,3,NA) mean(v1, na.rm = TRUE) ## [1] 2

      There are more nuances and therefore, we will come back to the assignment in Chapter 4.4.4 “Assignment Operators” on page 78. These nuances are better understood with some more background, and for now it is enough to be able to assign values to variable.

      Variable Management

      # List all variables ls() # hidden variable starts with dot ls(all.names = TRUE) # shows all # Remove a variable rm(x.1) # removes the variable x.1 ls() # x.1 is not there any more rm(list = ls()) # removes all variables ls()

      image Note – What are invisible variables

      A variable whose name starts with a dot (e.g. .x) is in all aspects the same as a variable that starts with a letter. The only difference is that the first will be hidden with the standard arguments of the function ls().

       ls()

       rm()

      4.3.1 The Elementary Types

      There is no need to declare variables explicitly and tell R what type the variable will be before using it. R will assign them a class whenever this is needed and even change the type when our code implies a change.

       class()

       long

       complex numbers

       string

      # Booleans can be TRUE or FALSE: x <- TRUE class(x) ## [1] "logical" # Integers use the letter L (for Long integer): x <- 5L class(x) ## [1] "integer" # Decimal numbers, are referred to as ‘numeric’: x <- 5.135 class(x) ## [1] "numeric" # Complex numbers use the letter i (without multiplication sign): x <- 2.2 + 3.2i class(x) ## [1] "complex" # Strings are called ‘character’: x <- "test" class(x) ## [1] "character"

      image Warning – Changing data types

      While R allows to change the type of a variable, doing so is not a good practice. It makes code difficult to read and understand.

      # Avoid this: x <- 3L # x defined as integer x ## [1] 3 x <- "test" # R changes data type x ## [1] "test"

      So, keep your code tidy and to not change data types.

      Dates

       date

      # The function as.Data coerces its argument to a date d <- as.Date(c("1852-05-12", "1914-11-5", "2015-05-01")) # Dates will work as expected d_recent <- subset(d, d > as.Date("2005-01-01")) print(d_recent) ## [1] "2015-05-01"

       as.Date()

       subset()

      image Further information –More about dates

      Make sure to read Section 17.6 “Dates with lubridate” on page 407 for more information about working with dates as well as the inevitable problems related to dates.

      4.3.2 Vectors

      4.3.2.1 Creating Vectors

      Simply put, vectors are lists of objects that are all of the same type. They can be the result of a calculation or be declared with the function c().

       vector

      x <- c(2, 2.5, 4, 6) y <- c("apple", "pear") class(x) ## [1] "numeric" class(y) ## [1] "character"

      More about lists can be found in Section 4.3.6 "Lists" on page 53.

      4.3.3 Accessing Data from a Vector

      # Create v as a vector of the numbers one to 5: v <- c(1:5) # Access elements via indexing: v[2] ## [1] 2 v[c(1,5)] ## [1] 1 5 # Access via TRUE/FALSE: v[c(TRUE,TRUE,FALSE,FALSE,TRUE)] ## [1] 1 2 5 # Access elements via names: v <- c("pear" = "green", "banana" = "yellow", "coconut" = "brown") v ## pear banana coconut ## "green" "yellow" "brown" v["banana"] ## banana ## "yellow" # Leave out certain elements: v[c(-2,-3)] ## pear ## "green"

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