The Big R-Book. Philippe J. S. De Brouwer
Чтение книги онлайн.
Читать онлайн книгу The Big R-Book - Philippe J. S. De Brouwer страница 26
<-
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
With what we have seen so far, it is possible to make already simple calculations, define and modify variables. There is still a lot to follow and it is important to have some basic tools to keep things tidy. One of such tools is the possibility to see defined variables and eventually remove unused ones.
# 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()
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 Data Types
As most computer languages, R has some built-in data-types. While it is possible to do certain things in R without worrying about data types, understanding and consciously using these base-types will help you to write bug-free code that is more robust and it will certainly speed up the debugging process. In this section we will highlight the most important ones.
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"
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
Working with dates is a complex subject. We explain the essence of the issues in Section 17.6 “Dates with lubridate” on page 407. For now, it is sufficient to know that dates are one of the base types of R.
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()
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
In many situations you will need to address a vector as a whole, but there will also be many occasions where it is necessary to do something with just one element. Since this is such a common situation, R provides more than one way to get this done.
# 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"